4

I was using the code posted in answer here Switch between two frames in tkinter by @Bryan Oakley. Now I want to add some menu bar. In Pageone I want to add menus File and Help and in Pagetwo I want to add menus Data and plot But I cant get it done.

I can add menus to to the main window. But then it is available to all the pages and doesn't go away when I change the page. I don't want that. I want to add menubar specific to that page. How can I do that

EDIT

Actually what i want to do that is that, I am making a GUI where you can do mathematical integration and matrix calculation and so on. So on opening the GUI there will be window with buttons for Integration, matrix etc. And clicking on those will open a window where you can do corresponding calculation. I did it using the Toplevel() command which opens up a new window and I just hide the previous. But I liked GUI code posted by Bryan in that link, so I asked about adding menu to individual pages.

In the Bryan's code I just added these lines `class SampleApp(tk.Tk):

def __init__(self, *args, **kwargs):
    tk.Tk.__init__(self, *args, **kwargs)
    menu=tk.Menu(self)
    self.config(menu=menu)
    subMenu=tk.Menu(menu)
    menu.add_cascade(label="File",menu=subMenu)`

But as I said it attach the menu to the main window. But I want different menu for different pages.

Community
  • 1
  • 1
Eular
  • 1,707
  • 4
  • 26
  • 50
  • 1
    doesn't this go against the normal use of a menu bar? – Tadhg McDonald-Jensen Jun 03 '16 at 18:21
  • 3
    and just change the menubar every time the `show_frame` method in that example is called, if you had a map of the different menus it would likely be as simple as `self.config(menu=self.menus[page_name])` – Tadhg McDonald-Jensen Jun 03 '16 at 18:23
  • 1
    How did you create the menus? Who is their parent, Is it the appropriate frame or maybe the root window form? Maybe you should provide some example code. – rocksteady Jun 03 '16 at 18:25

1 Answers1

6

The simplest solution is to add a method to every page that creates the menubar for that page, and then switch to that menu when you switch frames.

This will likely require you to have a bunch of duplicate code for all of the menu items that will be common between pages (eg: edit->cut, edit-copy, edit-paste, etc), but it at least shows the general technique.

For example, a page might define a method like this:

class PageOne(tk.Frame):
    ...
    def menubar(self, root):
        menubar = tk.Menu(root)
        pageMenu = tk.Menu(menubar)
        pageMenu.add_command(label="PageOne")
        menubar.add_cascade(label="PageOne", menu=pageMenu)
        return menubar

You would then call this method when you switch frames, like this:

def SampeApp(tk.Tk):
    ...
    def show_frame(self, page_name):
        '''Show a frame for the given page name'''
        frame = self.frames[page_name]
        frame.tkraise()

        menubar = frame.menubar(self)
        self.configure(menu=menubar)
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685