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

在wxPython中,如何定期重新绘制?,wxpython,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, s

我希望我的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

你必须在画布上放些东西

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

你必须在画布上放些东西

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