Friday, September 5, 2025
HomeLanguageswxPython – ToggleTool() function in wx.ToolBar

wxPython – ToggleTool() function in wx.ToolBar

In this article we are going to learn about ToggleTool() function associated with the wx.ToolBar class of wxPython. ToggleTool() function is used to toggle a tool on or off. This does not cause any event to get emitted. It takes two parameters that are toolId and toggle.
 

Syntax:

wx.ToolBar.ToggleTool(self, toolId, toggle)

Parameters:

Parameter Input Type Description
toolId int ID of the tool in question, as passed to AddTool .
toggle bool If True, toggles the tool on, otherwise toggles it off.

Code Example 1:

Python3




import wx
  
class Example(wx.Frame):
  
    def __init__(self, *args, **kwargs):
        super(Example, self).__init__(*args, **kwargs)
        self.InitUI()
  
    def InitUI(self):
        self.locale = wx.Locale(wx.LANGUAGE_ENGLISH)
        self.toolbar = self.CreateToolBar()
  
        td = self.toolbar.AddTool(1, 'right', wx.Bitmap('right.png'), kind = wx.ITEM_CHECK)
        te = self.toolbar.AddTool(2, 'wrong', wx.Bitmap('wrong.png'))
        self.toolbar.Realize()
        self.Bind(wx.EVT_TOOL, self.OnOne, td)
  
        self.SetSize((350, 250))
        self.SetTitle('Undo redo')
        self.Centre()
  
    def OnOne(self, e):
        # Toggle tool using ToggleTool() function
        self.toolbar.ToggleTool(toolId = 1, toggle = True)
        # Realize() called to finalize new added tools
        self.toolbar.Realize()
  
    def OnQuit(self, e):
        self.Close()
  
  
def main():
  
    app = wx.App()
    ex = Example(None)
    ex.Show()
    app.MainLoop()
  
  
if __name__ == '__main__':
    main()


Output: 
Before clicking tool: 
 

After clicking tool: 
 

 

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32265 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6634 POSTS0 COMMENTS
Nicole Veronica
11801 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11863 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6701 POSTS0 COMMENTS
Umr Jansen
6718 POSTS0 COMMENTS