Open In App

wxPython – Add image with menuitem in wx.MenuBar

Improve
Improve
Like Article
Like
Save
Share
Report

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:



Last Updated : 04 Jun, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads