wxpython类中的定时器wx.Timer方法不能运行,不知缺少了什么命令代码,请求指教,谢谢!
import wx
class MyFrame(wx.Frame):
eve = 1
def __init__(self):
super().__init__(parent=None,title="vbox",size=(500,400),pos=(100,100)) #继承wx.Frame类
self.Center()
self.panel = wx.Panel(parent=self) #面板
vbox = wx.BoxSizer(wx.VERTICAL)
self.bmps=[wx.Bitmap(f"{i}.png",wx.BITMAP_TYPE_PNG) for i in range(1,6)]
self.image=wx.StaticBitmap(self.panel,-1,self.bmps[0])
self.timer=wx.Timer(self.panel)
b1=wx.Button(self.panel,id=1,label='自动播放')
self.Bind(wx.EVT_BUTTON,self.on_click,id=1,id2=5)
self.Bind(wx.EVT_TIMER,self.keep_play,self.timer)
vbox.Add(b1,1,wx.CENTER)
vbox.Add(self.image,5,wx.CENTER)
self.panel.SetSizer(vbox)
def on_click(self,event):
event_id = event.GetId()
if event_id==1:
self.timer.Start(100)
def keep_play(self,event):
if self.eve<5:
self.image.SetBitmap(self.bmps[self.eve])
else:
self.eve=0
self.image.SetBitmap(self.bmps[self.eve])
self.eve=self.eve+1
class App(wx.App):
def OnInit(self): #进入
frame = MyFrame()
frame.Show()
return True
def OnExit(self): #退出
print("tuichu")
return 0
if __name__ == '__main__':
app=App()
app.MainLoop()