Skip to content
Related Articles

Related Articles

wxPython – Add image with menuitem in wx.MenuBar
  • Last Updated : 04 Jun, 2020

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:

ParameterInput TypeDescription
bmpwx.BitmapBitmap we need to use with menuitem.
checkedboolIf 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:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :