Models Views and Controllers

Using a StringVar to connect a text-entry box and a label is the first step toward separating models, views, and controllers, which is the key to building larger GUis (and many other kinds of applications). As its name suggests, a view is something that displays information to the user, like Label.

Figure 14.8: An entry and a label tied together

Many views, like Entry, also accept input, which they display immediately. The key is that they don't do anything else: they don't calculate average temperatures, move robot arms, or do any other calculations.

Models, on the other hand, just store data, like a piece of text or the current inclination of a telescope. They also don't do calculations; their job is simply to keep track of the application's current state (and, in some cases, to save that state to a file or database and reload it later).

Sitting beside an application's models and views are its controllers, implementing its intelligence. The controller is what decides whether two gene sequences match well enough to be colored green or whether someone is allowed to overwrite an old results file. Controllers may update an application's models, which in turn can trigger changes to its views.

The following code shows what all of this looks like in practice. Here, the model is a simple integer counter, which is implemented as an IntVar so that the view will update itself automatically. The controller is the function click, which updates the model whenever a button is clicked. Four objects make up the view: the root window, a Frame, a Label that shows the current value of counter, and a button that the user can click to increment counter's value:

Download gui/

# Initialization, from Tkinter import *

# The controller, def click():

counter.set(counter.get() + 1)

# More initialization, window = Tk()

frame = Frame(window) frame.pack()

button = Button(frame, text="Click", command=click) button.pack()

label = Label(frame, textvariable=counter) label.pack()


The first two arguments used to construct the Button should be familiar by now. The third, command=click, tells it to call the function click each time the user presses the button. This makes use of the fact that, in Python, a function is just another kind of object and can be passed as an argument like anything else.

The click function in the previous code does not have any parameters but uses the variable counter that is defined outside the function. Variables like this are called global variables, and their use should be avoided, since they make programs hard to understand. It would be better to pass any variables the function needs into it as parameters. We can't do this using the tools we have seen so far, because the functions that our buttons can call must not have any parameters. We will show you one way to avoid using global variables in the next section, and we'll show you another in Section 13.1, Class Color, on page 271.

Was this article helpful?

0 0

Post a comment