This tutorial will take you further through the concepts of Tkinter as continued from Part 1. We will use Menu and Text widgets to create a simple Text Editor GUI.
Here is a code example that shows the ease of setting up a menu. The comments should explain the code as it goes along.
#! /usr/bin/python from Tkinter import * class App: def __init__(self): self.root = Tk() self.root.title("Notpad") self.root.minsize(width=500,height=400) # Set up basic Menu menubar = Menu(self.root) # Add a command that sits on the menu and does something # At the moment, when you click this button, it does # nothing, but we'll get into that later, for right now, # open it and see your result. menubar.add_command(label="Button") self.root.config(menu=menubar) app = App() app.root.mainloop()
Here, we made a simple button on a menu that is at the top of the Tkinter frame.
Let me explain what all this does. In __init__, we initiate a new Tk Frame and set the title and minimum size. Then the root menu is added. In this program, we only add a command button to it, but in other programs we can add other menus such as “File”, “Edit”, and so on.
The code menubar.add_command adds a command button with the label specified as the label argument. Also we can add a callback function to this as I will show later.
Finally, the code self.root.config(menu=menubar) edits the root frame after it has been created. This code segment specifies that the root frame's menu equals the menubar that we just made.
It is just as easy to set up a series of menus as it is with just one menu. Following, we will set up a File menu and an Edit menu. Add this right after we declare menubar.
# Set up a separate menu that is a child of the main menu filemenu = Menu(menubar,tearoff=0) filemenu.add_command(label="New File", accelerator="Ctrl+N") filemenu.add_command(label="Open", accelerator="Ctrl+O") filemenu.add_separator() filemenu.add_command(label="Save", accelerator="Ctrl+S") filemenu.add_command(label="Save As", accelerator="Ctrl+Shift+S") filemenu.add_separator() filemenu.add_command(label="Quit", accelerator="Ctrl+Q") menubar.add_cascade(label="File", menu=filemenu) # Set up another menu that is also the child of main menu editmenu = Menu(menubar, tearoff=0) editmenu.add_command(label="Cut", accelerator="Ctrl+X") editmenu.add_command(label="Copy", accelerator="Ctrl+C") editmenu.add_command(label="Paste", accelerator="Ctrl+V") menubar.add_cascade(label="Edit", menu=editmenu)
We declare a menu that is the child of menubar and the tearoff argument says that this menu cannot be torn off of the application.
Then we add commands and separators as usual. One other thing that you may notice is the keyword accelerator. This makes the visual effect so that it displays whatever is between quotes as right justified.
Then we add it with the command add_cascade()
Finally to make the button physically do something, we must add a command argument
…. filemenu.add_command(label="New File", accelerator="Ctrl+N", command=self.hello) …. def hello(): print(“hello”)
One thing to notice is the fact that the function in the command does NOT have parentheses.