[问题] 我想停止调用函数的线程。如何停止wxpython中的线程?
[CODE]
import wx
app = wx.App(redirect=False)
top = wx.Frame(None)
sizer = wx.GridBagSizer()
import threading
shutdown_event = threading.Event()
def testFunction(event):
while not shutdown_event.is_set():
import time
for i in range(2):
print ('win','r')
time.sleep (0.5)
print ('abc')
time.sleep (0.5)
print ('enter')
time.sleep (0.5)
print 'sleep'
time.sleep (3)
def startThread(event):
import threading
th = threading.Thread(target=testFunction, args=(event,))
th.start()
def stopThread(event):
shutdown_event.set()
addButton = wx.Button(top, -1, "Start", style=wx.BU_EXACTFIT)
sizer.Add(addButton, (6, 8), (2, 14), wx.EXPAND)
stopButton = wx.Button(top, -1, "Stop", style=wx.BU_EXACTFIT)
sizer.Add(stopButton, (8, 9), (2, 14), wx.EXPAND)
top.Bind(wx.EVT_BUTTON, startThread, addButton)
top.Bind(wx.EVT_BUTTON, stopThread, stopButton)
top.Sizer = sizer
top.Sizer.Fit(top)
top.Show()
app.MainLoop()
[CURRENT]如果我点击 “停止” 按钮 什么也没有发生。
[DESIRED] 如果点击“停止”按钮,函数“testFunction”应该停止。
我理解你的观点。我的目标是当我点击停止btn时停止功能。换句话说,如果该功能总共有5个步骤,并且当该功能在步骤3时单击“停止”,则不应再执行步骤4。 – george
然后你必须在每一步之前添加一个“break”或者“continue”。 – tglaria
[plus1],因为我懒得查找如何最好的摆脱''while''循环:D – nepix32