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/self.Centre()self.Show(True)def OnScroll(self, event):self.sel = event.GetInt()self.cpu.Refresh()app = wx.App()CPUWidget(None, -1, 'cpu')app.MainLoop()Creating this widget is quite simple. We create a black panel. <strong>The</strong>n wedraw small rectangles onto this panel. <strong>The</strong> color of the rectangles dependon the value of the slider. <strong>The</strong> color can be dark green or bright green.dc.SetDeviceOrigin(0, 100)dc.SetAxisOrientation(True, True)Here we change the default coordinate system to cartesian. This is tomake the drawing intuitive.pos = self.parent.GetParent().GetParent().selrect = pos / 5Here we get the value of the sizer. We have 20 rectangles in eachcolumn. <strong>The</strong> slider has 100 numbers. <strong>The</strong> rect parameter makes aconvertion from slider values into rectangles, that will be drawn in brightgreen color.for i in range(1, 21):if i > rect:dc.SetBrush(wx.Brush('#075100'))dc.DrawRectangle(10, i*4, 30, 5)dc.DrawRectangle(41, i*4, 30, 5)else:dc.SetBrush(wx.Brush('#36ff27'))dc.DrawRectangle(10, i*4, 30, 5)dc.DrawRectangle(41, i*4, 30, 5)Here we draw 40 rectangles, 20 in each column. If the number of therectangle being drawn is greater than the converted rect value, we drawit in a dark green color. Otherwise in bright green.8 de 9 27/04/2008 1:07

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

Saved successfully!

Ooh no, something went wrong!