Open In App

wxPython – InsertTool() function in wx.ToolBar

Last Updated : 10 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article we are going to learn about InsertTool() function associated with wx.ToolBar class of wxPython. InsertTool() function is the new style of inserting a tool in the toolbar as a particular position. InsertTool() takes arguments associated with a tool as its parameters.

Syntax: wx.ToolBar.InsertTool (self, pos, toolId, label, bitmap, bmpDisabled=NullBitmap, kind=ITEM_NORMAL, shortHelp=””, longHelp=””, clientData=None) Parameters:

Parameter Input Type Description
pos int Position to add tools starting from 0.
toolid int An integer by which the tool may be identified in subsequent operations.
label string The string to be displayed with the tool.
bitmap wx.bitmap The primary tool bitmap.
bmpDisabled wx.bitmap The bitmap used when the tool is disabled.
kind int kind of toolbar.
shortHelp string This string is used for the tools tooltip.
longHelp string detailed string associated with tool.
clientData PyUserData An optional pointer to client data which can be retrieved later using GetToolClientData.

Return Type: wx.ToolBarToolBase

Code Example: 

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, '', wx.Bitmap('sep.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):
        # insert tool with id = 2
        self.toolbar.InsertTool(pos = 1, toolId = 2, label ='wrong',
                                   bitmap = wx.Bitmap('wrong.png'))
        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 separate tool: after clicking separate tool: Code Example: 

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, '', wx.Bitmap('sep.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):
        # insert two tools in one go
        self.toolbar.InsertTool(pos = 3, toolId = 2, label ='wrong',
                                   bitmap = wx.Bitmap('wrong.png'))
        self.toolbar.InsertTool(pos = 4, toolId = 3, label ='right',
                                    bitmap = wx.Bitmap('right.png'))
        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 separate tool: after clicking separate tool:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads