wxPython – Replace() function in wxPython

Another function in wx.MenuBar class is Replace() function. If we ever want to replace a menu from menubar we use Replace() function. It takes three main parameters that is position of menu we want to replace, menu we want to add, title of new menu.

Syntax :

wx.MenuBar.Replace(self, pos, menu, title)

Parameters :

The title of the menu.

Parameter Input Type Description
pos int The position of the new menu in the menu bar
menu wx.Menu The menu to add.
title string The menu to add.

Let’s create a window with two menu items Menu_one and Menu_two.



Code :

filter_none

edit
close

play_arrow

link
brightness_4
code

import wx
  
  
class Example(wx.Frame):
  
    def __init__(self, *args, **kw):
        super(Example, self).__init__(*args, **kw)
  
        # create MenuBar using MenuBar() function
        menubar = wx.MenuBar()
  
        # add menu to MenuBar
        fm1 = wx.Menu()
        fileitem = fm1.Append(20, "one")
  
        fm2 = wx.Menu()
        fileitem2 = fm2.Append(20, "two")
        menubar.Append(fm1, '&Menu_one')
        menubar.Append(fm2, '&Menu_two')
        self.SetMenuBar(menubar)
        self.SetSize((300, 200))
        self.SetTitle('Menu Bar')
          
def main():
    app = wx.App()
    ex = Example(None)
    ex.Show()
    app.MainLoop()
  
  
if __name__ == '__main__':
    main()

chevron_right


Window:

Now lets replace Menu_two with new_Menu.

Code for Replace :

filter_none

edit
close

play_arrow

link
brightness_4
code

import wx
  
  
class Example(wx.Frame):
  
    def __init__(self, *args, **kw):
        super(Example, self).__init__(*args, **kw)
  
        # create MenuBar using MenuBar() function
        menubar = wx.MenuBar()
  
        # add menu to MenuBar
        fm1 = wx.Menu()
        fileitem = fm1.Append(20, "one")
  
        fm2 = wx.Menu()
        fileitem2 = fm2.Append(21, "two")
        fm3 = wx.Menu()
        fileitem3 = fm3.Append(22, "new")
  
        menubar.Append(fm1, '&Menu_one')
        menubar.Append(fm2, '&Menu_two')
        self.SetMenuBar(menubar)
        self.SetSize((300, 200))
        self.SetTitle('Menu Bar')
        menubar.Replace(1, fm3, "new_Menu")
  
def main():
    app = wx.App()
    ex = Example(None)
    ex.Show()
    app.MainLoop()
  
  
if __name__ == '__main__':
    main()

chevron_right


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.




My Personal Notes arrow_drop_up

Hi there I am a student of CGC COE, Landran in CSE department and have a great interest in programming

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.