我正在创建一个wx应用程序,其中一个按钮应该在单击时进入循环动画。背景颜色是按钮上的动画。它可以运行几分钟,但几分钟后应用程序开始变得无法响应甚至有时会崩溃计算机。
对于应用程序的使用而言,动画能够运行很长一段时间非常重要。我正在使用计时器来执行动画。我已经尝试过常规清除直流,使用不同的帧速率和双缓冲,每次都得到相同的结果。
我的代码如下:
import wx
import os
# begin wxGlade: extracode
# end wxGlade
class LbDisplay(wx.Frame):
sixon = False
sevenon = False
eighton = False
nineon = False
tenon = False
elevenon = False
ID_TIMER = 1
Speed = 1
red = 255
anim = 'down'
def __init__(self, *args, **kwds):
# begin wxGlade: LbDisplay.__init__
kwds["style"] = wx.NO_BORDER | wx.NO_FULL_REPAINT_ON_RESIZE
wx.Frame.__init__(self, *args, **kwds)
self.timer = wx.Timer(self, LbDisplay.ID_TIMER)
self.button_6 = wx.Button(self, -1, "")
self.button_7 = wx.Button(self, -1, "")
self.button_8 = wx.Button(self, -1, "")
self.button_9 = wx.Button(self, -1, "")
self.button_10 = wx.Button(self, -1, "")
self.button_11 = wx.Button(self, -1, "")
self.button_11.Bind(wx.EVT_BUTTON, self.close)
self.button_6.Bind(wx.EVT_BUTTON, self.change_six)
self.Bind(wx.EVT_TIMER, self.blink, id=LbDisplay.ID_TIMER)
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.__set_properties()
self.__do_layout()
# end wxGlade
def __set_properties(self):
# begin wxGlade: LbDisplay.__set_properties
self.SetTitle("LbDisplay")
# end wxGlade
def __do_layout(self):
# begin wxGlade: LbDisplay.__do_layout
self.SetMinSize(wx.GetDisplaySize())
grid_sizer_1 = wx.GridSizer(6, 1, 0, 0)
grid_sizer_1.Add(self.button_6, 0, wx.EXPAND, 0)
grid_sizer_1.Add(self.button_7, 0, wx.EXPAND, 0)
grid_sizer_1.Add(self.button_8, 0, wx.EXPAND, 0)
grid_sizer_1.Add(self.button_9, 0, wx.EXPAND, 0)
grid_sizer_1.Add(self.button_10, 0, wx.EXPAND, 0)
grid_sizer_1.Add(self.button_11, 0, wx.EXPAND, 0)
self.SetSizer(grid_sizer_1)
grid_sizer_1.Fit(self)
self.Layout()
# end wxGlade
def change_six(self, e):
if LbDisplay.sixon == False:
LbDisplay.sixon = True
self.timer.Start(LbDisplay.Speed)
else:
self.button_6.SetBackgroundColour(wx.Colour(223, 220, 217, 0))
self.timer.Stop()
LbDisplay.sixon = False
def close(self,e):
os._exit(True)
def blink(self,e):
if LbDisplay.anim == 'down':
LbDisplay.red -= 1
self.button_6.SetBackgroundColour(wx.Colour(LbDisplay.red,0,0))
if LbDisplay.red <= 56:
LbDisplay.anim = 'up'
if LbDisplay.anim == 'up':
LbDisplay.red += 1
self.button_6.SetBackgroundColour(wx.Colour(LbDisplay.red,0,0))
if LbDisplay.red >= 255:
LbDisplay.anim = 'down'
def OnPaint(self, e):
self.dc = wx.PaintDC(self)
# end of class LbDisplay
if __name__ == "__main__":
app = wx.PySimpleApp(0)
wx.InitAllImageHandlers()
LbDisplay = LbDisplay(None, -1, "")
app.SetTopWindow(LbDisplay)
LbDisplay.Show()
app.MainLoop()
任何想法?
答案 0 :(得分:0)
我自己想通了。如果您将观察到上述代码,您会注意到计时器使用的刷新率为300fps。我甚至认为GPU不能超过60fps,而且直接在CPU上运行。大声笑! 将它减少到20fps就可以了。现在它可以顺利运行一整天。