在本文中,我們將學習與wxPython的wx.ToolBar類關聯的GetToolState()函數。 GetToolState()函數獲取切換工具的打開/關閉狀態。如果打開工具,則返回True,否則返回False。它僅使用toolId作為標識Tool的參數。
用法:
wx.ToolBar.GetToolState(self, toolId)
參數:
參數 | 輸入類型 | 描述 |
---|---|---|
toolid | int | 一個整數,可以在隨後的操作中標識該工具。 |
返回類型:
bool
代碼示例:
import wx
class Example(wx.Frame):
def __init__(self, *args, **kwargs):
super(Example, self).__init__(*args, **kwargs)
self.InitUI()
def InitUI(self):
self.count = 5
self.locale = wx.Locale(wx.LANGUAGE_ENGLISH)
self.toolbar = self.CreateToolBar()
tundo = self.toolbar.AddTool(wx.ID_UNDO, '', wx.Bitmap('right.png'))
tredo = self.toolbar.AddTool(wx.ID_REDO, '', wx.Bitmap('wrong.png'))
self.toolbar.EnableTool(wx.ID_REDO, False)
self.toolbar.AddSeparator()
self.toolbar.Realize()
self.txt = wx.StaticText(self, 121, "Enabled")
self.Bind(wx.EVT_TOOL, self.OnUndo, tundo)
self.Bind(wx.EVT_TOOL, self.OnRedo, tredo)
self.SetSize((350, 250))
self.SetTitle('Undo redo')
self.Centre()
def OnUndo(self, e):
if(self.toolbar.GetToolState(wx.ID_UNDO)== True):
self.txt.SetLabel("Enabled")
else:
self.txt.SetLabel("Disabled")
if self.count > 1 and self.count <= 5:
self.count = self.count - 1
if self.count == 1:
self.toolbar.EnableTool(wx.ID_UNDO, False)
if self.count == 4:
self.toolbar.EnableTool(wx.ID_REDO, True)
def OnRedo(self, e):
if self.count < 5 and self.count >= 1:
self.count = self.count + 1
if self.count == 5:
self.toolbar.EnableTool(wx.ID_REDO, False)
if self.count == 2:
self.toolbar.EnableTool(wx.ID_UNDO, True)
def OnQuit(self, e):
self.Close()
def main():
app = wx.App()
ex = Example(None)
ex.Show()
app.MainLoop()
if __name__ == '__main__':
main()
輸出:
代碼示例2:
import wx
class Example(wx.Frame):
def __init__(self, *args, **kwargs):
super(Example, self).__init__(*args, **kwargs)
self.InitUI()
def InitUI(self):
self.count = 5
self.locale = wx.Locale(wx.LANGUAGE_ENGLISH)
self.toolbar = self.CreateToolBar()
tundo = self.toolbar.AddTool(wx.ID_UNDO, '', wx.Bitmap('right.png'))
tredo = self.toolbar.AddTool(wx.ID_REDO, '', wx.Bitmap('wrong.png'))
self.toolbar.EnableTool(wx.ID_REDO, False)
self.toolbar.AddSeparator()
self.toolbar.Realize()
self.txt = wx.StaticText(self, 121, "Enabled")
self.Bind(wx.EVT_TOOL, self.OnUndo, tundo)
self.Bind(wx.EVT_TOOL, self.OnRedo, tredo)
self.SetSize((350, 250))
self.SetTitle('Undo redo')
self.Centre()
def OnUndo(self, e):
if(self.toolbar.GetToolState(wx.ID_UNDO)== True):
print(True)
else:
print(False)
if self.count > 1 and self.count <= 5:
self.count = self.count - 1
if self.count == 1:
self.toolbar.EnableTool(wx.ID_UNDO, False)
if self.count == 4:
self.toolbar.EnableTool(wx.ID_REDO, True)
def OnRedo(self, e):
if self.count < 5 and self.count >= 1:
self.count = self.count + 1
if self.count == 5:
self.toolbar.EnableTool(wx.ID_REDO, False)
if self.count == 2:
self.toolbar.EnableTool(wx.ID_UNDO, True)
def OnQuit(self, e):
self.Close()
def main():
app = wx.App()
ex = Example(None)
ex.Show()
app.MainLoop()
if __name__ == '__main__':
main()
輸出:
True False
相關用法
- wxPython wx.StaticText IsEllipsized()用法及代碼示例
- wxPython wx.StaticText SetBackgroundColour()用法及代碼示例
- wxPython wx.StaticText GetLabel()用法及代碼示例
- wxPython FindControl()用法及代碼示例
- wxPython wx.StaticText SetForegroundColour()用法及代碼示例
- wxPython GetToolByPos()用法及代碼示例
- wxPython GetClassDefaultAttributes()用法及代碼示例
- wxPython wx.ToolBar AddTool()用法及代碼示例
- wxPython FindToolForPosition()用法及代碼示例
- wxPython GetToolBitmapSize()用法及代碼示例
- wxPython GetMargins()用法及代碼示例
注:本文由純淨天空篩選整理自RahulSabharwal大神的英文原創作品 wxPython | GetToolState() function in wx.ToolBar。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。