2017-06-10 3 views
0

GUIを毎秒リフレッシュ(再描画)したいので、定期的にdraw_all()を呼び出すタイマーを設定します。しかし、実際にはキャンバスには何も描画されません。誰でもその理由を知っていますか?またはこれを行うためのより良い方法は?wxPythonでは、定期的に再ペイントする方法は?

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 

答えて

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() 
関連する問題