0
我正在製作一個wx應用程序,其中的按鈕應該在點擊時進入循環動畫。背景顏色是按鈕上的動畫。它在幾分鐘內運行良好,但是在幾分鐘後該應用程序開始變得沒有反應,甚至有時會使計算機崩潰。
動畫能夠運行很長時間對應用程序的使用非常重要。我正在使用計時器來執行動畫。我已經嘗試了常規清除直流電,使用不同的幀速率和雙緩衝,並且每次都得到相同的結果。wxPython - 按鈕動畫會減慢計算機的速度
我的代碼如下:
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()
什麼想法?