wxPython - GetKind() function in wx.MenuItem

wxPython - GetKind() function in wx.MenuItem

In wxPython, wx.MenuItem represents an item in a menu. A menu item can be of different kinds: a normal item, a separator, a check item, or a radio item.

The GetKind() method of wx.MenuItem returns an integer that indicates the kind of the menu item. The return values can be:

  • wx.ITEM_NORMAL: It's a regular item.
  • wx.ITEM_SEPARATOR: It's a separator.
  • wx.ITEM_CHECK: It's a checkable item.
  • wx.ITEM_RADIO: It's a radio item.

Here's a simple example to demonstrate the use of the GetKind() method with wx.MenuItem:

import wx class MyFrame(wx.Frame): def __init__(self, parent, title): super(MyFrame, self).__init__(parent, title=title, size=(300, 200)) self.InitUI() def InitUI(self): menubar = wx.MenuBar() fileMenu = wx.Menu() normalItem = fileMenu.Append(wx.ID_NEW, 'Normal Item') checkItem = fileMenu.AppendCheckItem(wx.ID_OPEN, 'Check Item') radioItem1 = fileMenu.AppendRadioItem(wx.ID_SAVE, 'Radio Item 1') radioItem2 = fileMenu.AppendRadioItem(wx.ID_SAVEAS, 'Radio Item 2') fileMenu.AppendSeparator() exitItem = fileMenu.Append(wx.ID_EXIT, 'Exit') menubar.Append(fileMenu, '&File') self.SetMenuBar(menubar) self.Bind(wx.EVT_MENU, self.OnMenuSelect) def OnMenuSelect(self, event): item = self.GetMenuBar().FindItemById(event.GetId()) kind = item.GetKind() if kind == wx.ITEM_NORMAL: print("Normal Item selected") elif kind == wx.ITEM_CHECK: print("Check Item selected") elif kind == wx.ITEM_RADIO: print("Radio Item selected") elif kind == wx.ITEM_SEPARATOR: print("Separator selected") event.Skip() app = wx.App() frame = MyFrame(None, 'MenuItem Kind') frame.Show() app.MainLoop() 

In this example, when a menu item is selected, the OnMenuSelect method gets triggered, which then retrieves the kind of the menu item using the GetKind() method and prints a corresponding message.


More Tags

word-wrap ora-00933 manytomanyfield android-widget square xml-nil spline pymongo constants vgg-net

More Programming Guides

Other Guides

More Programming Examples