Wednesday, May 6, 2026
HomeLanguageswxPython | InsertControl() function in wx.ToolBar

wxPython | InsertControl() function in wx.ToolBar

In this article, we are going to learn about InsertControl() function associated with wx.ToolBar class of wxPython. InsertControl() inserts the control into the toolbar at the given position. Note that You must call Realize for the change to take place.

Syntax :

wx.ToolBar.InsertControl(self, pos, control, label="")

Parameters:

Parameter Input Type Description
pos int Position where to insert control starting from 0.
control wx.Control Control which you want to insert.
label string Label to be shown on control.

Return Type:

wx.ToolBarToolBase

Code Example 1: on clicking tick tool on toolbar a control is inserted.




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()
        tundo = self.toolbar.AddTool(1, '', wx.Bitmap('right.png'))
        tredo = self.toolbar.AddTool(2, '', wx.Bitmap('wrong.png'))
  
        self.toolbar.Realize()
        self.Bind(wx.EVT_TOOL, self.OnOne, tundo)
  
        self.SetSize((350, 250))
        self.SetTitle('Undo redo')
        self.Centre()
  
    def OnOne(self, e):
        ctrl = wx.Control(self.toolbar, id = 1, size =(100, -1),
        style = 0,  name ="control")
        # insert control in toolbar at position 2
        self.toolbar.InsertControl(pos = 2, control = ctrl, label ="Control")
        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 tick:

after clicking tick:

Code Example 2: on clicking tick tool on toolbar a control is inserted.




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()
        tundo = self.toolbar.AddTool(1, '', wx.Bitmap('right.png'))
        tredo = self.toolbar.AddTool(2, '', wx.Bitmap('wrong.png'))
  
        self.toolbar.Realize()
        self.Bind(wx.EVT_TOOL, self.OnOne, tundo)
  
        self.SetSize((350, 250))
        self.SetTitle('Undo redo')
        self.Centre()
  
    def OnOne(self, e):
        ctrl = wx.Control(self.toolbar, id = 1, size =(100, -1),
        style = 0,  name ="control")
        # insert control in toolbar at position 2
        self.toolbar.InsertControl(pos = 0, control = ctrl, label ="Control")
        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 tick:

after clicking tick:

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

Most Popular

Dominic
32513 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6886 POSTS0 COMMENTS
Nicole Veronica
12008 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12104 POSTS0 COMMENTS
Shaida Kate Naidoo
7016 POSTS0 COMMENTS
Ted Musemwa
7261 POSTS0 COMMENTS
Thapelo Manthata
6974 POSTS0 COMMENTS
Umr Jansen
6960 POSTS0 COMMENTS