Open In App

wxPython - InsertTool() function in wx.ToolBar

Last Updated : 10 Oct, 2022
Comments
Improve
Suggest changes
Like Article
Like
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:

ParameterInput TypeDescription
posintPosition to add tools starting from 0.
toolidintAn integer by which the tool may be identified in subsequent operations.
labelstringThe string to be displayed with the tool.
bitmapwx.bitmapThe primary tool bitmap.
bmpDisabledwx.bitmapThe bitmap used when the tool is disabled.
kindintkind of toolbar.
shortHelpstringThis string is used for the tools tooltip.
longHelpstringdetailed string associated with tool.
clientDataPyUserDataAn 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:


Next Article
Article Tags :
Practice Tags :

Similar Reads