Log in / Register
Home arrow Computer Science arrow Data Structures and Algorithms with Python

Implementing a GUI with Tkinter

The word GUI means Graphical User Interface. Implementing a Graphical User Interface in Python is very easy using a module called Tkinter. The Tcl/Tk language and toolkit was designed as a cross-platform method of creating GUI interfaces. Python provides an interface to this toolkit via the Tkinter module.

A GUI is an event-driven program. This means that you write your code to respond to events that occur in the program. The events occur as a result of mouse clicks, dragging the mouse, button presses, and menu items being selected.

To build a GUI you place widgets in a window. Widgets are any element of a GUI like labels, buttons, entry boxes, and sometimes invisible widgets called frames. A frame is a widget that can hold other widgets. The drawing application you see in Fig. 1.6 is one such GUI built with Tkinter. In this section we'll develop this drawing application so you learn how to create your own GUI applications using Tkinter and to improve or refresh your Python programming skills.

Fig. 1.6 The Draw Program

To construct a GUI you need to create a window. It is really very simple to do this using Tkinter.

root = tkinter.Tk()

This creates an empty window on the screen, but of course does not put anything in it. We need to place widgets in it so it looks like the window in Fig. 1.6 (without the nice picture that Denise drew for us; thanks Denise!). We also need to create event handlers to handle events in the drawing application.

Putting widgets in a window is called layout. Laying out a window relies on a layout manager of some sort. Windowing toolkits support some kind of layout. In Tkinter you either pack, grid, or place widgets within a window. When you pack widgets it's like packing a suitcase and each widget is stacked either beside or below the previous widget packed in the GUI. Packing widgets will give you the desired layout in most situations, but at times a grid may be useful for laying out a window. The place layout manager lets you place widgets at a particular location within a window. We'll use the pack layout manager to layout our drawing application.

When packing widgets, to get the proper layout, sometimes you need to create a Frame widget. Frame widgets hold other widgets. In Fig. 1.7 two frame widgets have been created. The DrawingApplication frame is the size of the whole window and holds just two widgets that are placed side by side within it: the canvas and the sideBar frame. A canvas is a widget on which a turtle can draw. The sideBar widget holds all the buttons, entry boxes, and labels.

Fig. 1.7 The Draw Program Layout

The DrawingApplication frame inherits from Frame. When programming in an object-oriented language, sometimes you want to implement a class, but it is almost like another class. In this case, the DrawingApplication is a Frame. This means there are two parts to DrawingApplication objects, the Frame part of the DrawingApplication and the rest of it, which in this case is the PyList sequence of graphics commands. Our frame will keep track of the graphics commands that are used to draw the picture on the canvas. Portions of the code appear in Sect. 1.14.1. The code in Sect. 1.14.1 shows you all the widgets that are created and how they are packed within the window.

The canvas and the sideBar widgets are added side by side to the Drawing Application frame. Then all the entry, label, and button widgets are added to the sideBar frame.

In addition, there is a menu with the Draw application. The menu is another widget that is added to the window (called self.master in the code in Sect. 1.14.1). The fileMenu is what appears on the menu bar. The menu items “New”, “Load...”, “Load Into...”, “Save As...”, and “Exit” are all added to this menu. Each menu item is linked to an event handler that is executed when it is selected.

When theTurtle object is created in Sect. 1.14.1, it is created as a RawTurtle. A RawTurtle is just like a turtle except that a RawTurtle can be provided a canvas to draw on. A Turtle object creates its own canvas when the first turtle is created. Since we already have a canvas for the turtle, we create a RawTurtle object.

In addition to the event handlers for the widgets, there are three other event handlers. The onclick event occurs when you click the mouse button on the canvas. The ondrag event handler occurs when the turtle is dragged around the canvas. Finally, the undoHandler is called when the u key is pressed on the keyboard.

Found a mistake? Please highlight the word and press Shift + Enter  
Next >
Business & Finance
Computer Science
Language & Literature
Political science