How can I make a progress bar

If you want to graphically display a numeric value without allowing the user to change it, use the relevant wxPython widget wx.Gauge. An example of this numeric value is a progress bar, displayed in figure 7.9.

Listing 7.9 displays the code that created this figure. Unlike many other examples in this chapter, in this example we added an event handler. The following code adjusts the value of the gauge during idle time, causing it to loop from start to finish and back again.

Progressbar Wxpython
Figure 7.9 A wx.Gauge displaying some progress

Listing 7.9 Displaying and updating a wx.Gauge import wx class GaugeFrame(wx.Frame):

wx.Frame._init_(self, None, -1, 'Gauge Example', size=(350, 150)) panel = wx.Panel(self, -1) self.count = 0

self.gauge = wx.Gauge(panel, -1, 50, (20, 50), (250, 25))



self.Bind(wx.EVT_IDLE, self.OnIdle)

def OnIdle(self, event):

self.count = self.count + 1 if self.count >= 50:

self.count = 0 self.gauge.SetValue(self.count)

if __name__ == '__main__': app = wx.PySimpleApp() GaugeFrame().Show() app.MainLoop()

The constructor for wx.Gauge is similar to the other numerical widgets:

wx.Gauge(parent, id, range, pos=wx.DefaultPosition, size=wx.DefaultSize, style=wx.GA_HORIZONTAL, validator=wx.DefaultValidator, name="gauge")

As you enter the numerical value using the range argument, it represents the upper bound of the gauge, while the lower bound is always 0. The default style, wx.ga_horizontal provides a horizontal bar. To rotate it 90 degrees, you use the style wx.GA_VERTlCAL. If you are on Windows, the style wx.GA_PROGRESSBAR gives you the native progress bar from the Windows toolkit.

As a read-only control, wx.Gauge has no events. However, it does have properties you can set. You can adjust the value and range using GetValue(), Set-Value(pos), GetRange(), and SetRange(range). If you are on Windows, and are not using the native progress bar style, you can use SetBezelFace(width) and Set-ShadowWidth() to change the width of the 3D effect.

Was this article helpful?

0 -2


  • rowan
    How to create and use progress bar in wxpython?
    8 years ago
  • Cailyn
    How to print message using progress bar in wxpython?
    8 years ago

Post a comment