Friday, December 12, 2025
HomeLanguageswxPython – Dynamically enable and disable tools in toolbar using button

wxPython – Dynamically enable and disable tools in toolbar using button

In this article we are going to learn how can we enable and disable dynamically using a single button. We will create a button and a toolbar with a single tool. We will use a normal button like a toggle button.

Steps :

1. Create a toolbar in a frame.
2. Add a tool with a certain id.
3. Create button in the window.
4. Declare a global count variable and initialize it to zero.
5. Increment when button is clicked.
6. If count is even then enable tool else disable tool.

Code Example:




import wx
  
  
class Example(wx.Frame):
  
    def __init__(self, *args, **kwargs):
        super(Example, self).__init__(*args, **kwargs)
  
        self.InitUI()
  
    def InitUI(self):
        pnl = wx.Panel(self)
        self.toolbar = self.CreateToolBar()
        qtool = self.toolbar.AddTool(12, 'Quit'
                     wx.Bitmap('/Desktop/wxPython/signs.png'))
        self.toolbar.Realize()
  
        self.Bind(wx.EVT_TOOL, self.OnQuit, qtool)
  
        self.SetSize((350, 250))
        self.SetTitle('Simple toolbar')
        self.Centre()
        self.btn = wx.Button(pnl, label ='Close', pos =(20, 20))
  
        self.btn.Bind(wx.EVT_BUTTON, self.Onclick)
  
        self.SetSize((350, 250))
        self.SetTitle('wx.Button')
        self.Centre()
  
  
    def OnQuit(self, e):
        self.Close()
  
    def Onclick(self, e):
        # disable tool using EnableTool
        self.toolbar.EnableTool(12, False)
  
  
def main():
  
    app = wx.App()
    ex = Example(None)
    ex.Show()
    app.MainLoop()
  
  
if __name__ == '__main__':
    main()


Output :

Enabled :

Disabled :

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

1 COMMENT

Most Popular

Dominic
32445 POSTS0 COMMENTS
Milvus
105 POSTS0 COMMENTS
Nango Kala
6813 POSTS0 COMMENTS
Nicole Veronica
11952 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12028 POSTS0 COMMENTS
Shaida Kate Naidoo
6949 POSTS0 COMMENTS
Ted Musemwa
7198 POSTS0 COMMENTS
Thapelo Manthata
6893 POSTS0 COMMENTS
Umr Jansen
6881 POSTS0 COMMENTS