wxPython Gauge Experiment

vegaseat 0 Tallied Votes 389 Views Share

The wxPython widget wx.Gauge is commonly called a progress bar. In this code snippet I have bound the wx.Gauge to a wx.Slider, as you move the slider so do the indicating bars of the gauge. A second method is explored too, here the gauge indicates the progress of a timed loop. Hopefully there is enough information to create your own applications.

# experiment with wxPython's wx.Gauge
# wxGauge is read-only and generates no events
# wx.Gauge(parent, id, range, position_tuple, size_tuple, style)
# position_tuple (x, y) of upper left corner, size_tuple (width, height)
# (on my Windows XP box the slider responds to the mouse-wheel, when it has the focus)
# tested with Python24 and wxPython26     vegaseat     18oct2005

import wx
import time

class MyPanel(wx.Panel):
    class MyPanel creates a panel with a slider, button and a gauge (progress bar)
    this class inherits wx.Panel
    (putting your components/widgets on a panel increases versatility)
    def __init__(self, parent, id):
        # create a panel
        wx.Panel.__init__(self, parent, id)

        # 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), (390, 50),
            wx.SL_HORIZONTAL | wx.SL_AUTOTICKS | wx.SL_LABELS)
        # default id = -1 is used, range is 0 to 100, default is horizontal style
        self.gauge1 = wx.Gauge(self, -1, 100, (10, 70), (390, 25))
        # set gauge to initial slider position
        self.pos1 = self.slider1.GetValue()
        str1 = "the gauge is bound to the slider, just click and drag the pointer"
        self.label1 = wx.StaticText(self, -1, str1, (10, 100))
        str2 = "the gauge can be driven with a timer loop"
        self.label2 = wx.StaticText(self, -1, str2, (10, 140))
        self.button1 = wx.Button(self, -1, "Press to start timer", (10, 170))
        # respond to changes in slider position ...
        self.slider1.Bind(wx.EVT_SLIDER, self.sliderUpdate)
        # respond to the click of the button
        self.button1.Bind(wx.EVT_BUTTON, self.button1Click)
    def sliderUpdate(self, event):
        # get the slider position
        self.pos1 = self.slider1.GetValue()
        # set the gauge position
    def button1Click(self, event):
        """starts a timed loop 0 to 100"""
        for k in range(101):
            # move the progress bar/gauge
            # move the slider too for the fun of it
            # 0.015 second intervals

app = wx.PySimpleApp()
# create a window/frame, no parent, -1 is default ID, title, size
frame = wx.Frame(None, -1, "wxGauge Test1", size = (420, 310))
# call the derived class, -1 is default ID
# show the frame
# start the event loop
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.