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.
wxPython Custom Gauge
# wxPython:
# experiment with a colorful gauge/progress-bar made from a label
# (note wx.Gauge() comes only in green)
# tested with Python24 and wxPython26 vegaseat 21feb2006
import wx
class MyPanel(wx.Panel):
"""
class MyPanel creates a panel with a slider, a gauge/progress bar
made from a label and a variable number of spaces
"""
def __init__(self, parent, id):
# create a panel
wx.Panel.__init__(self, parent, id)
self.SetBackgroundColour("white")
# default id = -1 is used, initial value = 50, min value = 0, max value = 100
self.slider1 = wx.Slider(self, -1, 50, 0, 100, (10, 10), (400, 50),
wx.SL_HORIZONTAL | wx.SL_AUTOTICKS | wx.SL_LABELS)
# set gauge to initial slider position
self.pos1 = self.slider1.GetValue()
# this label is used for a simple colorful gauge
# str3 has 0 to 100 spaces, each space is one tickmark
# you might have to factor that for different sizes
str3 = " " * self.pos1
self.label3 = wx.StaticText(self, -1, str3, (10, 80))
self.label3.SetBackgroundColour("blue")
# respond to changes in slider position ...
self.slider1.Bind(wx.EVT_SLIDER, self.sliderUpdate)
def sliderUpdate(self, event):
# get the slider position
self.pos1 = self.slider1.GetValue()
# optionally change the color of the label-gauge
if self.pos1 > 80:
self.label3.SetBackgroundColour("red")
else:
self.label3.SetBackgroundColour("blue")
# set the label-gauge position
# str3 contains 0 to 100 spaces
str3 = " " * self.pos1
self.label3.SetLabel(str3)
app = wx.PySimpleApp()
# create a window/frame, no parent, -1 is default ID, title, size
frame = wx.Frame(None, -1, "test the label-gauge (drag slider)", size = (430, 150))
# call the derived class, -1 is default ID
MyPanel(frame,-1)
# show the frame
frame.Show(True)
# start the event loop
app.MainLoop()
naytanl 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.