Using Menus in Tkinter

This is a quick post on how to use menu’s in Tkinter, if you are unfamiliar with Tkinter you might want to check out some of my other Tkinter related posts.

We are going to start of with a basic python Tkinter app:

#! /usr/bin/env python
from Tkinter import *

class App(Frame):

	def __init__(self, master):
		
		"""Initialise the base class"""
        	Frame.__init__(self,master)
        
        	"""Set the Window Title"""
        	self.master.title("TkInter Menus")
		self.configure(height=200,width=200)
        	"""Display the main window
        	with a little bit of padding"""
        	self.grid(padx=15, pady=15,sticky=N+S+E+W)

if __name__ == "__main__":
	root = Tk()
	app = App(root)
	root.mainloop()

Python Tkinter WindowWhat this gives us is basically a plain old blank Tkinter window, not too exciting. The next thing we are going to do is add create our root menu, before going any further you might want to read the excellent information about menu widgets in PythonWare’s Tkinter introduction. You don’t have to but it’s an excellent source of information.

To create the root menu we simply add the following command to our __init__ function:

#Create the Menu base
self.menu = Menu(self)
#Add the Menu
self.master.config(menu=self.menu)

No we are greeted with, well a blank window with a blank menu, the reason for this is because we haven’t added anything to it. What we are going to add to it is a menu, but not just any menu, a menu who’s parent is our menu base:

#Create our Python menu
self.tkMenu = Menu(self.menu)
#Add our Menu to the Base Menu
self.menu.add_cascade(label="TkMenu", menu=self.tkMenu)

Python Tkinter WindowNow we’re getting somewhere at least we have a menu now! The next step is to add items to our TkMenu:

#Add items to the menu
self.tkMenu.add_command(label="Simple", command=self.Simple)
self.tkMenu.add_separator()
self.tkMenu.add_command(label="Menu", command=self.Menu)

Python Tkinter MenuThat’s basically it! Adding the menu item’s is pretty simple, we specify it’s text and then tell it what function to call when the user clicks on the menu item. The two functions that I used don’t really do anything besides show a message box:

def Simple(self):
		tkMessageBox.showinfo("Simple", "Simple")
def Menu(self):
		tkMessageBox.showinfo("Menu", "Menu")

Well that’s it for this post, here is the code in its entirety:

#! /usr/bin/env python
from Tkinter import *

import tkMessageBox

class App(Frame):


	def __init__(self, master):
		
		"""Initialise the base class"""
        	Frame.__init__(self,master)
        
        	"""Set the Window Title"""
        	self.master.title("TkInter Menus")
		self.configure(height=200,width=200)
        	"""Display the main window
        	with a little bit of padding"""
        	self.grid(padx=15, pady=15,sticky=N+S+E+W)       
 
       		#Create the Menu base
        	self.menu = Menu(self)
        	#Add the Menu
        	self.master.config(menu=self.menu)
        	
		#Create our Python menu
		self.tkMenu = Menu(self.menu)
		#Add our Menu to the Base Menu
        	self.menu.add_cascade(label="TkMenu", menu=self.tkMenu)

		#Add items to the menu
		self.tkMenu.add_command(label="Simple", command=self.Simple)
		self.tkMenu.add_separator()
		self.tkMenu.add_command(label="Menu", command=self.Menu)
	
	def Simple(self):
		tkMessageBox.showinfo("Simple", "Simple")
	def Menu(self):
		tkMessageBox.showinfo("Menu", "Menu")        	


if __name__ == "__main__":
	root = Tk()
	app = App(root)
	root.mainloop()

16 thoughts on “Using Menus in Tkinter”

  1. Having trouble with menus — specifically, I get a window, no menu. I’ve tried your example, pasting it in. Tried several others too. Same unsatisfactory result: A window, but no menu.

    I’m beginning to suspect that the current Python download is defective — how else to explain same result with at least 4 independent ‘text book examples’. I’d tell you what versions I’m using if I knew how to find it. (Learning my way around OSX and Unix at the same time. This is a Mac OSX10.4.7. The Python is ‘probably’ 2.4.

    A previous program, with buttons, still runs

    Thanks, JL

  2. Hi Joel,

    If you want to test what version of python you have installed on your system simply run “python” from the Terminal to launch the interactive editor. You should get a message like this that shows your version:


    Python 2.5 (r25:51918, Sep 19 2006, 08:49:13)
    [GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.

    (ctrl+d to quit)

    I’ve tested the code with version 2.4.2 and 2.5 and both work properly on my Mac. One thing to remember is that the menu’s will not show up on the window, they will appear in the “menu bar” along the top of the screen where menu’s appear on OS X.

    Take a look at the screenshot in the tutorial and I think you’ll see what I mean.

  3. Pingback: Las Noyas de Taran
  4. same problem as joel, on mac 10.4 using php 2.6.2 and the menu does not appear, copy and pasted several source code examples

  5. It worked perfectly.
    Configuration:
    Linux version 2.6.31.5-desktop-1mnb (herton@n2.mandriva.com) (gcc version 4.4.1 (GCC) ) #1 SMP Fri Oct 23 01:46:54 EDT 2009

    One issue, though:
    the line

    self.tkMenu = Menu(self.menu)

    should be

    self.tkMenu = Menu(self.menu,tearoff=0)

  6. Hi. Just exploring tk under OSX. Put up a menu – File, help, etc, works with callback.

    First thing I ran into, though, was a “Python” menu leading the others; if I’m to code an application, I can’t have that. I googled the heck out of the web, nothing found that even remotely helps.

    Is there a way to get rid of the “Python” menu and put up a “My Application” menu?

    …you’d think this would be the first thing addressed in any menu demo…

  7. Pingback: Ann-Lorrain
  8. Pingback: Alaine
  9. Pingback: Mecra.Org
  10. Pingback: Arlette

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>