wxPython | wx.Toolbar 中的 CreateTool()函数
在这篇特别的文章中,我们将学习 wxPython 中 wx.ToolBar 类中的 CreateTool()函数。 CreateTool()函数是用于创建新工具栏工具的工厂函数。 CreateTool()函数仅创建使用 AddTool()函数进一步添加的工具。
Syntax:
wx.ToolBar.CreateTool(self, id, label, bitmap, bmpDisabled=wx.NullBitmap, kind=wx.ITEM_NORMAL, clientData=None, shortHelp=””, longHelp=””)
Parameters :
Parameter | Input Type | Description |
---|---|---|
toolid | int | An integer by which the tool may be identified in subsequent operations. |
label | string | The string to be displayed with the tool. |
bmpNormal | wx.bitmap | The primary tool bitmap. |
bmpDisabled | wx.bitmap | The bitmap used when the tool is disabled. |
kind | int | kind of toolbar. |
clientData | PyUserData | An optional pointer to client data which can be retrieved later using GetToolClientData. |
shortHelp | string | This string is used for the tools tooltip. |
longHelp | string | detailed string associated with tool. |
例子:
import wx
class Example(wx.Frame):
global count
count = 0;
def __init__(self, *args, **kwargs):
super(Example, self).__init__(*args, **kwargs)
self.InitUI()
def InitUI(self):
pnl = wx.Panel(self)
self.toolbar = self.CreateToolBar()
# create a tool using CreateTool() function
self.ptool = self.toolbar.CreateTool(12,
'oneTool',
wx.Bitmap('path / wxPython / right.png'),
shortHelp ="Simple Tool")
self.btn = wx.Button(pnl, label ='Add created tool', pos =(20, 20))
self.btn.Bind(wx.EVT_BUTTON, self.Onclick)
self.toolbar.Realize()
self.SetSize((350, 250))
self.SetTitle('Control')
self.Centre()
def Onclick(self, e):
# Add toolbar tool using AddTool() function
self.toolbar.AddTool(self.ptool)
self.btn.SetLabel("Added tool")
def main():
app = wx.App()
ex = Example(None)
ex.Show()
app.MainLoop()
if __name__ == '__main__':
main()
输出:
在开始申请时:
单击按钮时: