Sunday, February 10, 2019

tkinter example - 14 (Menu Widget)

The Menu widget is used to implement menus used by an application the most frequently used being toplevel, pulldown, and popup menus. Menus in GUIs are presented with a combination of text and symbols to represent the choices. Selecting with the mouse (or finger on touch screens) on one of the symbols or text, an action will be started. Such an action or operation can, for example, be the opening or saving of a file, or the quitting or exiting of an application. As this widget uses native code where ever possible, we shouldn’t try to fake menus using buttons and other Tkinter widgets.

Let's create a Top level menu which is displayed just under the title bar of the root or any other toplevel windows (or on Macintosh, along the upper edge of the screen). To create a toplevel menu, create a new Menu instance, and use add methods to add commands and other menu entries to it.

See the following program:

from tkinter import *

root = Tk()

def greeting():
    print ("Howdy!")

# create a toplevel menu
menubar = Menu(root)
menubar.add_command(label="Howdy!", command=greeting)
menubar.add_command(label="Quit!", command=root.quit)

# display the menu
root.config(menu=menubar)
mainloop()

The output of the program is shown below:



This simple program creates a top level menu containing two labels on the menu bar, Howdy and Quit. If we click Howdy, a greeting Howdy! is printed on the command window. If we click Quit the program ends.


Next we'll create a Pull Down menu are created in a similar fashion. The main difference is that they are attached to a parent menu (using add_cascade), instead of a toplevel window. See the following program:

from tkinter import *
from tkinter.filedialog import *

def open_callback():
filename = askopenfilename()
print(filename)

def saveas_callback():
filename = asksaveasfilename()
print("File saved in desired format")


root = Tk()
root.title('Pull-down menu')
menu = Menu()
root.config(menu=menu)
file_menu = Menu(menu, tearoff=0)
file_menu.add_command(label='Open', command=open_callback)
file_menu.add_command(label='Save as', command=saveas_callback)
file_menu.add_separator()
file_menu.add_command(label='Exit', command=root.destroy)
menu.add_cascade(label='File', menu=file_menu)
mainloop()


The output of the program is shown below:



If we click Open, the file open dialog comes up:



We select the file and click the open button the command window shows the name of the file we opened:



If we select save as option:



We select the file and click the save button, the command window shows the name of the file we opened:



If we click Exit the program exits:



In order to create a Pop up menu we use the post method to explicitly display the menu, see the following program:

from tkinter import *
from tkinter.filedialog import *

root = Tk()
root.title('Pop Up menu')

def Redoing():
    print ("Done redoing!")
   
def Undoing():
    print ("Done undoing!")

# create a popup menu
menu = Menu(root, tearoff=0)
menu.add_command(label="Undo", command=Redoing)
menu.add_command(label="Redo", command=Undoing)

# create a canvas
frame = Frame(root, width=512, height=512)
frame.pack()

def popup(event):
    menu.post(event.x_root, event.y_root)

# attach popup to canvas
frame.bind("<Button-3>", popup)
root.config(menu=menu)
mainloop()

The output of the program is shown below:



Once the program is run it creates a Pop Up menu with two labels, Undo and Redo. Click the labels prints their respective messages on the command window.

Try to make programs using these menus to understand their utility. You may want to use classes to make better programs. I'll end this post now and till we meet next, keep learning and practicing Python as Python is easy to learn!

Share:

0 comments:

Post a Comment