13.07.2015 Views

The wxPython tutorial

The wxPython tutorial

The wxPython tutorial

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>The</strong> Graphics Device Interfacehttp://www.zetcode.com/wxpython/gdi/import wxclass CrossHair(wx.Frame):def __init__(self, parent, id, title):wx.Frame.__init__(self, parent, id, title, size=(250, 150))self.Bind(wx.EVT_MOTION, self.OnMotion)self.Bind(wx.EVT_LEAVE_WINDOW, self.OnLeaveWindow)self.SetBackgroundColour('WHITE')self.SetCursor(wx.StockCursor(wx.CURSOR_CROSS))self.Centre()self.Show(True)def DrawCrossHair(self, a, b):dc = wx.ClientDC(self)dc.Clear()dc.SetPen(wx.Pen(wx.Colour(100, 100, 100), 1, wx.DOT))dc.CrossHair(a, b)def OnMotion(self, event):x, y = event.GetPosition()self.DrawCrossHair(x, y)def OnLeaveWindow(self, event):dc = wx.ClientDC(self)dc.SetBackground(wx.Brush('WHITE'))dc.Clear()app = wx.App()CrossHair(None, -1, 'CrossHair2')app.MainLoop()self.Bind(wx.EVT_MOTION, self.OnMotion)self.Bind(wx.EVT_LEAVE_WINDOW, self.OnLeaveWindow)We bind two events to event handlers. <strong>The</strong> wx.EVT_MOTIONevent is generated, when we move a cursor over the window.<strong>The</strong> second event wx.EVT_LEAVE_WINDOW is generated,when we leave the window with our mouse cursor.def OnMotion(self, event):x, y = event.GetPosition()self.DrawCrossHair(x, y)Every time we move a cursor over a window, the methodOnMotion() is called. In this method we figure out the currentposition of the mouse cursor and call the DrawCrossHairmethod, which is responsible for drawing the cross hair.21 de 44 27/04/2008 1:08

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!