import wx
class DrawFrame(wx.Window):
def __init__(self,parent,ID):
wx.Window.__init__(self,parent,ID)
self.Bind(wx.EVT_LEFT_DOWN,self.OnLeftDown)
self.Bind(wx.EVT_LEFT_UP,self.OnLeftUp)
self.Bind(wx.EVT_MOTION,self.OnMotion)
def OnLeftDown(self,event):
self.pos=tuple(event.GetPosition())
self.CaptureMouse()
def OnLeftUp(self,event):
if self.HasCapture():
self.ReleaseMouse()
def OnMotion(self,event):
if event.Dragging() and event.LeftIsDown():
dc=wx.ClientDC(self)
self.drawMotion(dc,event)
event.Skip()
def drawMotion(self,dc,event):
self.pen=wx.Pen('red',1,wx.SOLID)
dc.SetPen(self.pen)
newPos=tuple(event.GetPosition())
coords=self.pos+newPos
print(coords)
dc.DrawLine(*coords)
self.pos=newPos
class MainFrame(wx.Frame):
def __init__(self,parent):
wx.Frame.__init__(self,parent,-1,'Draw Frame',size=(800,600))
self.frame=DrawFrame(self,-1)
if __name__=='__main__':
app=wx.App()
frame=MainFrame(None)
frame.Show(True)
app.MainLoop()
效果如下:
?
|