Please support our Python advertiser: Programming Forums
Feb 22nd, 2006
Views: 2,645
Here we experiment with a gauge or progress bar made from a label displaying a variable number of spaces in color. In this example, we use from 0 to 100 spaces. This label based gauge allows you to choose many different colors, and you can even change the color as the gauge reaches critical levels. The custom gauge is bound to a wx.Slider to test it.
python Syntax
  1. # wxPython:
  2. # experiment with a colorful gauge/progress-bar made from a label
  3. # (note wx.Gauge() comes only in green)
  4. # tested with Python24 and wxPython26 vegaseat 21feb2006
  5.  
  6. import wx
  7.  
  8. class MyPanel(wx.Panel):
  9. """
  10. class MyPanel creates a panel with a slider, a gauge/progress bar
  11. made from a label and a variable number of spaces
  12. """
  13. def __init__(self, parent, id):
  14. # create a panel
  15. wx.Panel.__init__(self, parent, id)
  16. self.SetBackgroundColour("white")
  17.  
  18. # default id = -1 is used, initial value = 50, min value = 0, max value = 100
  19. self.slider1 = wx.Slider(self, -1, 50, 0, 100, (10, 10), (400, 50),
  20. wx.SL_HORIZONTAL | wx.SL_AUTOTICKS | wx.SL_LABELS)
  21.  
  22. # set gauge to initial slider position
  23. self.pos1 = self.slider1.GetValue()
  24.  
  25. # this label is used for a simple colorful gauge
  26. # str3 has 0 to 100 spaces, each space is one tickmark
  27. # you might have to factor that for different sizes
  28. str3 = " " * self.pos1
  29. self.label3 = wx.StaticText(self, -1, str3, (10, 80))
  30. self.label3.SetBackgroundColour("blue")
  31.  
  32. # respond to changes in slider position ...
  33. self.slider1.Bind(wx.EVT_SLIDER, self.sliderUpdate)
  34.  
  35. def sliderUpdate(self, event):
  36. # get the slider position
  37. self.pos1 = self.slider1.GetValue()
  38.  
  39. # optionally change the color of the label-gauge
  40. if self.pos1 > 80:
  41. self.label3.SetBackgroundColour("red")
  42. else:
  43. self.label3.SetBackgroundColour("blue")
  44.  
  45. # set the label-gauge position
  46. # str3 contains 0 to 100 spaces
  47. str3 = " " * self.pos1
  48. self.label3.SetLabel(str3)
  49.  
  50.  
  51. app = wx.PySimpleApp()
  52. # create a window/frame, no parent, -1 is default ID, title, size
  53. frame = wx.Frame(None, -1, "test the label-gauge (drag slider)", size = (430, 150))
  54. # call the derived class, -1 is default ID
  55. MyPanel(frame,-1)
  56. # show the frame
  57. frame.Show(True)
  58. # start the event loop
  59. app.MainLoop()

Only community members can submit or comment on code snippets. You must register or log in to contribute.

Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 6:13 am.
Newsletter Archive - Sitemap - Privacy Statement - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC