📜  wxPython – 检查菜单内的项目

📅  最后修改于: 2022-05-13 01:54:39.659000             🧑  作者: Mango

wxPython – 检查菜单内的项目

在本文中,我们将学习在 Menubars 中的 Menu 中检查菜单项。我们将使用 Check()函数编写代码来显示和隐藏状态栏。

参数 :

ParameterInput TypeDescription
idintThe menu item identifier.
checkboolIf True, the item will be checked, otherwise it will be unchecked.

代码 :

import wx
  
  
class Example(wx.Frame):
  
    def __init__(self, *args, **kwargs):
        super(Example, self).__init__(*args, **kwargs)
  
        self.InitUI()
  
    def InitUI(self):
  
        menubar = wx.MenuBar()
        viewMenu = wx.Menu()
  
        self.showsb = viewMenu.Append(wx.ID_ANY, 'Show statusbar',
                                                 'Show Statusbar', 
                                            kind = wx.ITEM_CHECK)
  
        viewMenu.Check(self.showsb.GetId(), True)
   
        self.Bind(wx.EVT_MENU, self.shStatusBar, self.showsb)
  
        menubar.Append(viewMenu, '&View')
        self.SetMenuBar(menubar)
  
        self.statusbar = self.CreateStatusBar()
        self.statusbar.SetStatusText('This is statusbar')
  
        self.SetSize((450, 350))
        self.SetTitle('Check menu item')
        self.Centre()
  
  
    def shStatusBar(self, e):
  
        if self.showsb.IsChecked():
            self.statusbar.Show()
        else:
            self.statusbar.Hide()
  
def main():
  
    app = wx.App()
    ex = Example(None)
    ex.Show()
    app.MainLoop()
  
  
if __name__ == '__main__':
    main()

输出 :

检查:

未选中: