How to create a toggle button (Phoenix)

Keywords : Toggle button.


Introduction :

wx.ToggleButton is a button that has two states :

Pressed and not pressed.

You toggle between these two states by clicking on it.

There are situations where this functionality fits well.

wx.ToggleButton methods

SetValue(bool value)

set a state of a toggle button

bool GetValue()

get a state of the toggle button

SetLabel(string label)

set a label for the button

(info by ZetCode / Jan Bodnar).


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 
  12 # class MyDialog
  13 # class MyApp
  14 
  15 #---------------------------------------------------------------------------
  16 
  17 class MyDialog(wx.Dialog):
  18     def __init__(self, parent, id, title):
  19         wx.Dialog.__init__(self, parent, id, title, size=(300, 200))
  20 
  21         self.SetIcon(wx.Icon('./icons/wxwin.ico', wx.BITMAP_TYPE_ICO))
  22 
  23         #------------
  24 
  25         self.colour = wx.Colour(0, 0, 0)
  26 
  27         wx.ToggleButton(self, 1, '&Red', (20, 25))
  28         wx.ToggleButton(self, 2, '&Green', (20, 60))
  29         wx.ToggleButton(self, 3, '&Blue', (20, 100))
  30 
  31         self.panel  = wx.Panel(self, -1, (150, 20), (110, 110), style=wx.SUNKEN_BORDER)
  32         self.panel.SetBackgroundColour(self.colour)
  33 
  34         self.Bind(wx.EVT_TOGGLEBUTTON, self.ToggleRed, id=1)
  35         self.Bind(wx.EVT_TOGGLEBUTTON, self.ToggleGreen, id=2)
  36         self.Bind(wx.EVT_TOGGLEBUTTON, self.ToggleBlue, id=3)
  37 
  38         #------------
  39 
  40         self.Centre()
  41 
  42         #------------
  43 
  44         self.ShowModal()
  45         self.Destroy()
  46 
  47     #-----------------------------------------------------------------------
  48 
  49     def ToggleRed(self, event):
  50         green = self.colour.Green()
  51         blue = self.colour.Blue()
  52         if  self.colour.Red():
  53             self.colour.Set(0, green, blue)
  54         else:
  55             self.colour.Set(255, green, blue)
  56         self.panel.SetBackgroundColour(self.colour)
  57         self.panel.Refresh()
  58 
  59 
  60     def ToggleGreen(self, event):
  61         red = self.colour.Red()
  62         blue = self.colour.Blue()
  63         if  self.colour.Green():
  64             self.colour.Set(red, 0, blue)
  65         else:
  66             self.colour.Set(red, 255, blue)
  67         self.panel.SetBackgroundColour(self.colour)
  68         self.panel.Refresh()
  69 
  70 
  71     def ToggleBlue(self, event):
  72         red = self.colour.Red()
  73         green = self.colour.Green()
  74         if  self.colour.Blue():
  75             self.colour.Set(red, green, 0)
  76         else:
  77             self.colour.Set(red, green, 255)
  78         self.panel.SetBackgroundColour(self.colour)
  79         self.panel.Refresh()
  80 
  81 #---------------------------------------------------------------------------
  82 
  83 class MyApp(wx.App):
  84     def OnInit(self):
  85         frame = MyDialog(None, -1, 'wx.ToggleButton')
  86         frame.Show(True)
  87 
  88         return True
  89 
  90 #---------------------------------------------------------------------------
  91 
  92 def main():
  93     app = MyApp(redirect=False)
  94     app.MainLoop()
  95 
  96 #---------------------------------------------------------------------------
  97 
  98 if __name__ == "__main__" :
  99     main()


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 :

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


Comments

- blah, blah, blah....

How to create a toggle button (Phoenix) (last edited 2020-12-31 15:42:30 by Ecco)

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