以下代码应该创建一个由"校准"组成的窗口。按钮和画布。当"校准"单击按钮,红点应该在画布上的随机位置重新绘制。
相反,我看到OnPaint事件在开始时被调用一次,而不是之后。知道发生了什么吗?
import wx
import datetime
import threading
import random
class frmMain ( wx.Frame ):
def __init__( self, parent ):
wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 839,553 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
self.pos = (300,100)
self.initGUI()
def initGUI(self):
self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
topSizer = wx.BoxSizer( wx.HORIZONTAL )
buttonsSizer = wx.BoxSizer( wx.VERTICAL )
self.btnCalibrate = wx.Button( self, wx.ID_ANY, u"Calibrate", wx.DefaultPosition, wx.DefaultSize, 0 )
buttonsSizer.Add( self.btnCalibrate, 0, wx.ALL, 5 )
topSizer.Add( buttonsSizer, 0, wx.LEFT, 5 )
sizeCanvas = wx.BoxSizer( wx.VERTICAL )
sizeCanvas.SetMinSize( wx.Size( 600,600 ) )
self.panel=wx.Panel(self, size=(600,600))
self.panel.SetBackgroundColour('white')
self.firstpoint=wx.Point(300,300)
self.secondpoint=wx.Point(400,400)
self.panel.Bind(wx.EVT_PAINT, self.onPaint)
sizeCanvas.Add(self.panel, 0, wx.ALIGN_LEFT, 5)
topSizer.Add( sizeCanvas, 1, wx.ALIGN_RIGHT, 5 )
self.SetSizer( topSizer )
self.Layout()
self.Centre( wx.BOTH )
# Connect Events
self.btnCalibrate.Bind( wx.EVT_BUTTON, self.StartCalibrate )
self.Show(True)
def onPaint(self,event):
print "lalal"
dc = wx.WindowDC(self.panel)
color = wx.Colour(255,0,0)
b = wx.Brush(color)
dc.SetBrush(b)
dc.DrawCircle(self.pos[0], self.pos[1], 10)
def __del__( self ):
pass
def StartCalibrate( self, event ):
size = self.GetSize()
self.pos = (random.randrange(0, size[0] - 1, 1), random.randrange(0, size[1] - 1, 1))
print "fixation at %d, %d" % (self.pos[0], self.pos[1])
self.panel.Update()
event.Skip()
if __name__ == "__main__":
app = wx.App()
frmMain(None)
app.MainLoop()
答案 0 :(得分:1)
使用Refresh
代替Update
。 Update
会立即处理任何挂起的绘制事件,但如果没有挂起的绘制事件,则不会执行任何操作。另一方面,Refresh
会将绘制事件发送到窗口小部件。