12.07.2015 Views

Tkinter reference: A GUI for Python

Tkinter reference: A GUI for Python

Tkinter reference: A GUI for Python

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Part I: The face of your applicationWe’ll start by looking at the visible part of <strong>Tkinter</strong>: creating the widgets and arrangingthem on the screen. In Part II, below, we will talk about how to connect the face—the“front panel”—of the application to the logic behind it.1. A minimal applicationHere is a trivial <strong>Tkinter</strong> program containing only a Quit button:#!/usr/local/bin/pythonfrom <strong>Tkinter</strong> import *class Application(Frame):def __init__(self, master=None):Frame.__init__(self, master)self.grid()self.createWidgets()# Interface to Tk widgetsdef createWidgets(self):self.quitButton = Button ( self, text="Quit",command=self.quit )self.quitButton.grid()app = Application() # Instantiate the application classapp.master.title("Sample application")app.mainloop()# Wait <strong>for</strong> events2. Layout managementLater we will discuss the widgets, the building blocks of your <strong>GUI</strong> application. How dowidgets get arranged in a window?Although there are three different “geometry managers” in <strong>Tkinter</strong>, the author stronglyprefers the Grid geometry manager <strong>for</strong> pretty much everything. This manager treatsevery window or frame as a table—a gridwork of rows and columns. A cell is the area at the intersection of one row and one column. The width of each column is the width of the widest cell in that column. The height of each row is the height of the largest cell in that row. For widgets that do not fill the entire cell, you can specify what happens to the extraspace. You can either leave the extra space outside the widget, or stretch the widgetto fit it, in either the horizontal or vertical dimension. You can combine multiple cells into one larger area, a process called spanning.New Mexico Tech Computer Center <strong>Tkinter</strong> <strong>reference</strong>: Layout management Page 3

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!