wxPython – wx.ToolBar 中的 ToggleTool()函数
在本文中,我们将学习与 wxPython 的 wx.ToolBar 类关联的 ToggleTool()函数。 ToggleTool()函数用于打开或关闭工具。这不会导致任何事件被发出。它需要两个参数,即 toolId 和 toggle。
Syntax:
wx.ToolBar.ToggleTool(self, toolId, toggle)
参数:Parameter Input Type Description toolId int ID of the tool in question, as passed to AddTool . toggle bool If True, toggles the tool on, otherwise toggles it off.
代码示例 1:
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, 'right', wx.Bitmap('right.png'), kind = wx.ITEM_CHECK)
te = self.toolbar.AddTool(2, 'wrong', wx.Bitmap('wrong.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):
# Toggle tool using ToggleTool() function
self.toolbar.ToggleTool(toolId = 1, toggle = True)
# Realize() called to finalize new added tools
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()
输出:
点击工具前:
点击工具后: