CSCI 233 Python Exercise 9

This lab focuses on using some features of the Tkinter GUI module.

Basics

Recall that the basic model is that you create a bunch of widgets and place each in another widget (usually a frame), which we call its parent. You start off with a widget that is your root (sometimes “top-level”) window, also a frame.

It is the usual strategy to define a class corresponding to your program–and linking it to the root window. This object is usually called the application. Both the internal state of our program and the GUI state that we need to access will be reached from instance variables on this application object.

A widget can be unchanging, in which case it is created with fixed contents. A widget that changes–either from user input or from other program activity–will usually have its contents tied to a Tkinter variable. If we want a widget to respond to user action, we will associate an event-handling function with it.

Variables

Tkinter provides special “variable” objects that can be linked to widgets. A variable can be any of

    StringVar
    IntVar
    DoubleVar

When you create one of these variable objects, you’ll usually keep track of it with an instance variable. You access its value via its get() and set() methods. So you might have something like:

    self.v = StringVar()
    self.v.set("new value for v")
    ...connect the variable to a widget
    print self.v.get()

We’ll cover how to link a variable to a widget with the various widget types, below.

Events

When the user does something with a widget, such as press a button, that generates an event. When you create a widget, you can specify the function/method to handle events for it using the command option. In most cases, the handler will be a method on the application object or another object.

For example, you might see something like this in the initialization method for the application:

    ...
    b = Button(self.root, text="Button", command=self.press)
    ...

When this button is pressed, the method self.press will be called. That method should take one additional parameter, for the event object; so its definition might look like:

    def press(self, event):
        ... do something ...

It needs the self parameter because it is a method, and the event parameter to get details of the event. In simple cases, we’ll be able to ignore the value of event.

Widgets

Here are few of the basic widgets provided by Tkinter.

Frame

A frame is a container for other widgets. It is used when you want to group some widgets together for the purpose of placing them as a single item.

When we make a frame, there will usually be some object (class instance) with which it is associated. A good convention, then, is to make our class have the frame as an instance variable, with an accessor method getFrame() to get its frame.

Label

A label displays some text. If you want a fixed label, you can specify the text option; if you want to display varying text, you can associate the label with a variable using the textvariable option.

The main options for a label are:

Button

A button takes the same options as a label, plus one more:

Note that by default a button has relief=RAISED (instead of the default FLAT for a label).

Entry

An entry is a one-line box for typing text. It only makes sense to use it with the textvariable option. The options available from the list above are

Scale

A scale object gives a sliding numerical control; you link it with a Tkinter variable and it generates an event whenever the slider is moved. Primary options:

These optins affect the values:

These control appearance:

Grid layout

You place a widget inside its parent by calling the widget’s grid() method. The main options for that are:

You set sticky to say which walls (N+S+W+E) or corners (NE,SW,SE,NW) you want the widgit to stick to.

The root window and the application

You create the root window by calling Tk(). You can call the title() method on the root to set the window title. Your program runs under the interface when you call mainloop().

If you want to exit the application, call the destroy() method on the root window.

Trying it out

To get started, you might want to copy one of the in-class programs from Monday, which can be found in

/cslab/class/csci233/in-class/03-15/

These give you the basic structure of the program. You can then make a number of things.

Suggestions:

  1. Display a single label.
  2. Display two buttons, have one of them quit the program.
  3. Try linking together various forms of input and display to do conversions between the Fahrenheit and Celsius temperature scales. (This is particularly neat with a a pair of side-by-side sliders.)

Turning it in

There is nothing to turn in today.