创建了一个wx.Frame,面板有两个按钮,点击"start"按钮后文本不断显示随机数,可是点击“stop”按钮后却不能停止循环,不知道代码哪里出问题,请各位指点,感谢!
import wx
import random
import time
n=0
b=False
class MyFrame(wx.Frame):
def __init__(self):
super().__init__(None,title='random',size=(300,300),pos=(100,100))
panel=wx.Panel(parent=self)
self.st1=wx.StaticText(parent=panel,label='0',pos=(100,50))
self.st2=wx.StaticText(parent=panel,label='0',pos=(130,50))
self.b1=wx.Button(parent=panel,id=1,label='start',pos=(50,100))
self.b2=wx.Button(parent=panel,id=2,label='stop',pos=(150,100))
#self.Bind(wx.EVT_BUTTON,self.on_click,id=1,id2=10)
self.b1.Bind(wx.EVT_BUTTON,self.startBtn)
self.b2.Bind(wx.EVT_BUTTON,self.stopBtn)
def stopBtn(self,event):
b=False
def startBtn(self,event):
b=True
global n
while b:
n=random.randint(1,10)
self.st1.SetLabelText(str(n))
time.sleep(0.5)
if __name__=="__main__":
app=wx.App()
frm=MyFrame()
frm.Show()
app.MainLoop()