Next / Previous / Contents

15.2. Top-level menus

Especially under MacOS, it is sometimes desirable to create menus that are shown as part of the top-level window. To do this, follow these steps.

  1. Using any widget W, obtain the top-level window by using the W.winfo_toplevel() method.

  2. Create a Menu widget, using the top-level window as the first argument.

  3. Items added to this Menu widget will be displayed across the top of the application.

Here is a brief example. Assume that self is the application instance, an instance of a class that inherits from Frame. This code would create a top-level menu choice named “Help” with one choice named “About” that calls a handler named self.__aboutHandler:

    top = self.winfo_toplevel()
    self.menuBar = tk.Menu(top)
    top['menu'] = self.menuBar

    self.subMenu = tk.Menu(self.menuBar)
    self.menuBar.add_cascade(label='Help', menu=self.subMenu)
    self.subMenu.add_command(label='About', command=self.__aboutHandler)

There is some variation in behavior depending on your platform.