在wxPython中,如何定期重新绘制?

时间:2017-06-10 04:47:49

标签: wxpython

我希望我的GUI每秒刷新(重绘),所以我设置了一个定时器来定期调用我的draw_all()。但是,它实际上并没有在画布上绘制任何内容。谁知道原因?或者只是更好的方法来做到这一点?

def __init__(self):
        ...
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_PAINT, self.init_canvas)
        self.Bind(wx.EVT_TIMER, self.draw_all, self.timer)
        self.timer.Start(1000)
        self.Center()
        self.Show()

    def init_canvas(self, _):
        print('here')
        self._canvas = wx.PaintDC(self)

    def draw_all(self, _):
        print("there")
        self._canvas.do_stuff

1 个答案:

答案 0 :(得分:2)

你必须在画布上放些东西!

import wx
class Test(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, None, id, title)
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_PAINT, self.init_canvas)
        self.Bind(wx.EVT_TIMER, self.draw_all, self.timer)
        self.timer.Start(1000)
        self.Center()
        self.Show()
        self.Colour="RED"

    def init_canvas(self, _):
        print('here')
        self._canvas = wx.PaintDC(self)
        self._canvas.SetDeviceOrigin(30, 240)
        self._canvas.SetAxisOrientation(True, True)
        self._canvas.SetPen(wx.Pen(self.Colour))
        self._canvas.DrawRectangle(1, 1, 300, 200)

    def draw_all(self, _):
        if self.Colour == "RED":
            self.Colour = "BLUE"
        else:
            self.Colour = "RED"
        self._canvas.SetPen(wx.Pen(self.Colour))
        self._canvas.DrawRectangle(1, 1, 300, 200)
        print (self.Colour)

if __name__=='__main__':
    app = wx.App()
    frame = Test(parent=None, id=-1, title="Test")
    frame.Show()
    app.MainLoop()