13.07.2015 Views

The wxPython tutorial

The wxPython tutorial

The wxPython tutorial

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>The</strong> Graphics Device Interfacehttp://www.zetcode.com/wxpython/gdi/wx.Frame.__init__(self, parent, id, title, size=(250, 150))wx.FutureCall(2000, self.DrawLine)self.Centre()self.Show(True)def DrawLine(self):dc = wx.ClientDC(self)dc.DrawLine(50, 60, 190, 60)app = wx.App()Line(None, -1, 'Line')app.MainLoop()wx.FutureCall(2000, self.DrawLine)We call the DrawLine() method after the window has beencreated. We do it because, when the window is created, it isdrawn. All our drawings would be therefore lost. We can startdrawing after the window has been created. This is thereason, why we call the wx.FutureCall() method.def DrawLine(self):dc = wx.ClientDC(self)dc.DrawLine(50, 60, 190, 60)We create a wx.ClientDC device context. <strong>The</strong> only parameteris the window on which we want to draw. In our case it is self,which is a reference to our wx.Frame widget. We call theDrawLine() method of the device context. This call actuallydraws a line on our window.It is very important to understand the following behaviour. Ifwe resize the window, the line will disappear. Why is thishappening? Every window is redrawn, if it is resized. It is alsoredrawn, if it is maximized. <strong>The</strong> window is also redrawn, if wecover it by another window and uncover afterwards. <strong>The</strong>window is drawn to it's default state and our line is lost. Wehave to draw the line each time the window is resized. <strong>The</strong>solution is the wx.PaintEvent. This event is triggered everytime, the window is redrawn. We will draw our line inside amethod that will be hooked to the paint event.<strong>The</strong> following example shows how it is done.3 de 44 27/04/2008 1:08

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

Saved successfully!

Ooh no, something went wrong!