Home » Python Tkinter Menu

Python Tkinter Menu

by maxguy71

The tkinter menu is a top-level pulldown menu.

A lot of apps use a menu system for many functions such as file options like copy and paste

Syntax

mymenu = Menu(top, options)

A list of possible options is given below.

Option Description
activebackground The background color of the widget when the widget is under the focus.
activeborderwidth The width of the border of the widget when it is under the mouse. The default is 1 pixel.
activeforeground The font color of the widget when the widget has the focus.
bg The background color of the widget.
bd The border width of the widget.
cursor The mouse pointer is changed to the cursor type when it hovers the widget. The cursor type can be set to arrow or dot.
disabledforeground The font color of the widget when it is disabled.
font The font type of the text of the widget.
fg The foreground color of the widget.
postcommand The postcommand can be set to any of the function which is called when the mourse hovers the menu.
relief The type of the border of the widget. The default type is RAISED.
image It is used to display an image on the menu.
selectcolor The color used to display the checkbutton or radiobutton when they are selected.
tearoff By default, the choices in the menu start taking place from position 1. If we set the tearoff = 1, then it will start taking place from 0th position.
title Set this option to the title of the window if you want to change the title of the window.

Methods

The Menu widget contains the following methods.

Option Description
add_command(options) Adds a menu item to the menu.
add_radiobutton(options) Creates a radio button menu item.
add_checkbutton(options) This method is used to add the checkbuttons to the menu.
add_cascade(options) Create a hierarchical menu to the parent menu by associating the given menu to the parent menu.
add_seperator() Adds a separator line to the menu..
add(type, options) Adds the specific menu item to the menu.
delete(startindex, endindex) Delete the menu items exist in the specified range.
entryconfig(index, options) Configure a menu item identified by the given index.
index(item) It is used to get the index of the specified menu item.
insert_seperator(index) It is used to insert a seperator at the specified index.
invoke(index) It is used to invoke the associated with the choice given at the specified index.
type(index) It is used to get the type of the choice specified by the index.

 

Examples

In this example we implement a menu system with options similar to a lot of desktop apps

There are seperators, there are only 2 options available. In your app you would create this functionality.

We have an about option that displays a messagebox when that menu option is selected

Every other option simply displays a button with not completed yet in a new window

from tkinter  import *

def notimplemented():
   filenothing = Toplevel(root)
   button = Button(filenothing, text="Not completed yet")
   button.pack()

def about():
    messagebox.showinfo('Maxpython', 'Maxpython tkinter menu example')
   
root = Tk()
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="New", command=notimplemented)
filemenu.add_command(label="Open", command=notimplemented)
filemenu.add_command(label="Save", command=notimplemented)
filemenu.add_command(label="Save as...", command=notimplemented)
filemenu.add_command(label="Close", command=notimplemented)

filemenu.add_separator()

filemenu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=filemenu)
editmenu = Menu(menubar, tearoff=0)
editmenu.add_command(label="Undo", command=notimplemented)

editmenu.add_separator()

editmenu.add_command(label="Cut", command=notimplemented)
editmenu.add_command(label="Copy", command=notimplemented)
editmenu.add_command(label="Paste", command=notimplemented)
editmenu.add_command(label="Delete", command=notimplemented)
editmenu.add_command(label="Select All", command=notimplemented)

menubar.add_cascade(label="Edit", menu=editmenu)
helpmenu = Menu(menubar, tearoff=0)
helpmenu.add_command(label="Help Index", command=notimplemented)
helpmenu.add_command(label="About...", command=about)
menubar.add_cascade(label="Help", menu=helpmenu)

root.config(menu=menubar)
root.mainloop()

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More