wxPython – Add image with menuitem in wx.MenuBar

In this article we are going to learn that how can we add a bitmap image with the menuitem associated with the menu present in menubar object of wx.MenuBar class.
Steps :
1. Create a menubar object of wx.MenuBar class.
2. Create a menu object of wx.Menu class.
3. Create a MenuItem using wx.MenuItem class.
4. Use SetBitmap() function to add bitmap image with menu item.
Syntax of SetBitmap() method: wx.MenuItem.SetBitmap(self, bitmap, checked=True)
Parameters of SetBitmap() method:
Parameter Input Type Description bmp wx.Bitmap Bitmap we need to use with menuitem. checked bool If checkable set to True else False.
Code Example:
import wx APP_EXIT = 1 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) menubar = wx.MenuBar() fileMenu = wx.Menu() qmi = wx.MenuItem(fileMenu, APP_EXIT, '&Quit\tCtrl + Q') qmi.SetBitmap(wx.Bitmap('right.png')) fileMenu.Append(qmi) self.Bind(wx.EVT_MENU, self.OnQuit, id = APP_EXIT) menubar.Append(fileMenu, '&File') self.SetMenuBar(menubar) self.SetSize((350, 250)) self.SetTitle('Icons and shortcuts') self.Centre() def OnQuit(self, e): self.Close() def main(): app = wx.App() ex = Example(None) ex.Show() app.MainLoop() if __name__ == '__main__': main() |
Output:
<!–
–>

wxPython – Add Sub-Menu in menubar

wxPython – SetLabel() function in wx.MenuBar

wxPython – GetHelpString() function in wx.MenuBar

wxPython – SetHelpString() function in wx.MenuBar

wxPython – FindMenuItem() function in wx.MenuBar

wxPython – GetMenuLabelText() function in wx.MenuBar

wxPython – GetMenuLabel() function in wx.MenuBar

wxPython – GetMenuCount() function in wx.MenuBar

wxPython – GetLabel() function in wx.MenuBar

wxPython – Remove() function in wx.MenuBar




Please Login to comment…