wxPython wx.ToolBar中的GetToolState()函数

wxPython wx.ToolBar中的GetToolState()函数

在这篇文章中,我们将学习与wxPython的wx.ToolBar类有关的GetToolState()函数。GetToolState()函数获取一个切换工具的开/关状态。如果该工具被打开,它返回True,否则返回False。它只接受toolId作为一个参数来识别工具。

语法。

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()

输出:

wxPython  wx.ToolBar中的GetToolState()函数

wxPython  wx.ToolBar中的GetToolState()函数

代码示例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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

wxPython 教程