wxPython按钮动画会减慢计算机速度



我正在制作一个wx应用程序,其中一个按钮应该在单击时进入循环动画。背景颜色是在按钮上设置动画的颜色。它运行了几分钟很好,但几分钟后应用程序开始变得没有反应,有时甚至会导致计算机崩溃
对于应用程序的使用来说,动画能够长时间运行是很重要的。我正在使用计时器来执行动画。我尝试过定期清除dc,使用不同的帧速率和双缓冲,每次都得到相同的结果。

我的代码如下:

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()

有什么想法吗?

我自己想出来的。如果你观察上面的代码,你会注意到计时器使用的刷新率是300fps。我甚至不认为GPU可以做超过60fps的事情,而且这是直接在cpu上运行的。英雄联盟将其降低到每秒20帧就成功了。现在它可以顺利运行一整天了。

最新更新