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.

Creating custom widgetshttp://www.zetcode.com/wxpython/customwidgets/hbox3 = wx.BoxSizer(wx.HORIZONTAL)self.wid = Widget(panel, -1)hbox.Add(self.wid, 1, wx.EXPAND)hbox2.Add(CenterPanel, 1, wx.EXPAND)hbox3.Add(self.sld, 0, wx.TOP, 35)CenterPanel.SetSizer(hbox3)vbox.Add(hbox2, 1, wx.EXPAND)vbox.Add(hbox, 0, wx.EXPAND)self.Bind(wx.EVT_SCROLL, self.OnScroll)panel.SetSizer(vbox)self.sld.SetFocus()self.Centre()self.Show(True)def OnScroll(self, event):self.cw = self.sld.GetValue()self.wid.Refresh()app = wx.App()Burning(None, -1, 'Burning widget')app.MainLoop()All the important code resides in the OnPaint() method of the Widgetclass. This widget shows graphically the total capacity of a medium andthe free space available to us. <strong>The</strong> widget is controlled by a sliderwidget. <strong>The</strong> minimum value of our custom widget is 0, the maximum is750. If we reach value 700, we began drawing in red colour. Thisnormally indicates overburning.w, h = self.GetSize()self.cw = self.parent.GetParent().cw...till = (w / 750.0) * self.cwfull = (w / 750.0) * 700We draw the widget dynamically. <strong>The</strong> greater the window, the greaterthe burning widget. And vice versa. That is why we must calculate thesize of the wx.Panel onto which we draw the custom widget. <strong>The</strong> tillparameter determines the total size to be drawn. This value comes fromthe slider widget. It is a proportion of the whole area. <strong>The</strong> full parameterdetermines the point, where we begin to draw in red color. Notice theuse of floating point arithmetics. This is to achieve greater precision.<strong>The</strong> actual drawing consists of three steps. We draw the yellow or redand yellow rectangle. <strong>The</strong>n we draw the vertical lines, which divide thewidget into several parts. Finally, we draw the numbers, which indicatethe capacity of the medium.5 de 9 27/04/2008 1:07

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

Saved successfully!

Ooh no, something went wrong!