How to create a gauge (Phoenix)

Keywords : Gauge.


Introduction :

wx.Gauge is a widget that is used, when we process lengthy tasks.

wx.Gauge styles :

wx.Gauge methods

SetRange(integer range)

set the maximum value of the gauge

integer GetRange()

get the maximum value of the gauge

SetValue(integer position)

set the position of the gauge

integer GetValue()

get the position of the gauge

bool IsVertical()

check if the gauge is vertical

(info by JanBodnar / Zetcode).


Demonstrating :

Tested py3.x, wx4.x and Win10.

Are you ready to use some samples ? ;)

Test, modify, correct, complete, improve and share your discoveries ! (!)


Sample one

img_sample_one.png

   1 # sample_one.py
   2 
   3 """
   4 
   5 Author : Jan Bodnar
   6 Website : zetcode.com
   7 
   8 """
   9 
  10 import wx
  11 import random
  12 
  13 # class MyFrame
  14 # class MyApp
  15 
  16 #---------------------------------------------------------------------------
  17 
  18 class MyFrame(wx.Frame):
  19     def __init__(self, parent, id, title):
  20         wx.Frame.__init__(self, parent, id, title)
  21 
  22         self.SetIcon(wx.Icon('./icons/icon_wxWidgets.ico', wx.BITMAP_TYPE_ICO))
  23 
  24         #------------
  25 
  26         self.count = 0
  27         self.timer = wx.Timer(self, 1)
  28 
  29         #------------
  30 
  31         panel = wx.Panel(self, -1)
  32 
  33         self.gauge = wx.Gauge(panel, -1, 50, size=(250, 25))
  34         self.btn1 = wx.Button(panel, wx.ID_OK, "&Ok")
  35         self.btn2 = wx.Button(panel, wx.ID_STOP)
  36         self.text = wx.StaticText(panel, -1, 'Task to be done.')
  37 
  38         #------------
  39 
  40         vbox = wx.BoxSizer(wx.VERTICAL)
  41         hbox1 = wx.BoxSizer(wx.HORIZONTAL)
  42         hbox2 = wx.BoxSizer(wx.HORIZONTAL)
  43         hbox3 = wx.BoxSizer(wx.HORIZONTAL)
  44 
  45         hbox1.Add(self.gauge, 1, wx.ALIGN_CENTRE)
  46         hbox2.Add(self.btn1, 1, wx.RIGHT, 10)
  47         hbox2.Add(self.btn2, 1)
  48         hbox3.Add(self.text, 1)
  49 
  50         vbox.Add((0, 50), 0)
  51         vbox.Add(hbox1, 0, wx.ALIGN_CENTRE)
  52         vbox.Add((0, 30), 0)
  53         vbox.Add(hbox2, 1, wx.ALIGN_CENTRE)
  54         vbox.Add(hbox3, 1, wx.ALIGN_CENTRE)
  55 
  56         panel.SetSizer(vbox)
  57 
  58         #------------
  59 
  60         self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
  61         self.Bind(wx.EVT_BUTTON, self.OnOk, self.btn1)
  62         self.Bind(wx.EVT_BUTTON, self.OnStop, self.btn2)
  63 
  64         #------------
  65 
  66         self.Centre()
  67 
  68     #-----------------------------------------------------------------------
  69 
  70     def OnOk(self, event):
  71         if self.count >= 50:
  72             return
  73         self.timer.Start(100)
  74         self.text.SetLabel('Task in Progress.')
  75 
  76 
  77     def OnStop(self, event):
  78         if self.count == 0 or self.count >= 50 or not self.timer.IsRunning():
  79             return
  80         self.timer.Stop()
  81         self.text.SetLabel('Task Interrupted.')
  82         wx.Bell()
  83 
  84 
  85     def OnTimer(self, event):
  86         self.count = self.count +1
  87         self.gauge.SetValue(self.count)
  88         if self.count == 50:
  89             self.timer.Stop()
  90             self.text.SetLabel('Task Completed.')
  91 
  92 #---------------------------------------------------------------------------
  93 
  94 class MyApp(wx.App):
  95     def OnInit(self):
  96         frame = MyFrame(None, -1, 'wx.Gauge')
  97         frame.Show(True)
  98 
  99         return True
 100 
 101 #---------------------------------------------------------------------------
 102 
 103 app = MyApp(0)
 104 app.MainLoop()


Download source

source.zip


Additional Information

Link :

- - - - -

https://wiki.wxpython.org/TitleIndex

https://docs.wxpython.org/


Thanks to

Jan Bodnar (sample_one.py coding), the wxPython community...


About this page

Date(d/m/y) Person (bot) Comments :

15/12/20 - Ecco (Created page for wxPython Phoenix).


Comments

- blah, blah, blah....

How to create a gauge (Phoenix) (last edited 2020-12-30 17:44:10 by Ecco)

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.