我想使用wxTimer为面板内的蓝色框设置动画。但没有任何反应
这是我的代码:
#!/usr/bin/python
# -*- coding: iso-8859-15 -*-
import wx
WHITE_COLOR = (255,255,255)
class AnimationPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.boxParameters = [10,10,60,60]
self.SetBackgroundColour(wx.Colour(*WHITE_COLOR))
self.Bind(wx.EVT_PAINT, self.OnPaint)
timer = wx.Timer(self)
timer.Start(100)
self.Bind(wx.EVT_TIMER, self.OnTimer, timer)
def OnPaint(self, event):
dc = wx.PaintDC(self)
self.paintBox(dc)
def OnTimer(self, event):
self.boxParameters[0] += 3
self.Update()
def paintBox(self, dc):
dc.SetBrush(wx.Brush("blue"))
dc.DrawRectangle(*self.boxParameters)
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="Box with far and back movement", size=(300,200))
AnimationPanel(self)
if __name__ == "__main__":
app = wx.PySimpleApp()
frame = MainFrame()
frame.Show(True)
app.MainLoop()
提前致谢
答案 0 :(得分:3)
你遗漏了几件事。首先,一旦你到达 init 方法的末尾,wx.Timer就会超出范围,因此它甚至在它甚至无法执行任何操作之前就被销毁了。接下来,您希望使用Refresh()而不是Update(),因为Refresh()会将矩形(或整个屏幕)标记为“脏”并使其重新绘制。有关详细信息,请参阅文档:http://www.wxpython.org/docs/api/wx.Window-class.html
这是一个适用于我的Windows框的更新版本:
import wx
WHITE_COLOR = (255,255,255)
class AnimationPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.boxParameters = [10,10,60,60]
self.SetBackgroundColour(wx.Colour(*WHITE_COLOR))
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
self.timer.Start(100)
def OnPaint(self, event):
dc = wx.PaintDC(self)
self.paintBox(dc)
def OnTimer(self, event):
self.boxParameters[0] += 3
print self.boxParameters
self.Refresh()
def paintBox(self, dc):
dc.SetBrush(wx.Brush("blue"))
dc.DrawRectangle(*self.boxParameters)
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="Box with far and back movement", size=(300,200))
AnimationPanel(self)
if __name__ == "__main__":
app = wx.PySimpleApp()
frame = MainFrame()
frame.Show(True)
app.MainLoop()