The best way to learn how to do object-oriented programming is to go back through the examples and exercises of previous chapters and see which ones are easier or more naturally written using classes and objects. You can also create object-oriented programs that represent things in the real world. For example, what classes would you use to model the stars, planets, moons, rings, and comets that make up a solar system? What methods should each class have? How and where would you keep track of things like orbital parameters? How would you prevent programmers from accidentally putting a star in orbit around a comet or something equally silly?
Most of the programs in previous chapters are not interactive. Once launched, they run to completion without giving us a chance to steer them or provide new input. The few that do communicate with us do so through the kind of text-only command-line user interface, or CLUI, that would have already been considered old-fashioned in the early 1980s.
As you already know, most modern programs interact with users via a graphical user interface, or GUI, which is made up of windows, menus, buttons, and so on. in this chapter, we will show you how to build simple GUIs using a Python module called Tkinter. Along the way, we will introduce a different way of structuring programs called event-driven programming. A traditionally structured program usually has control over what happens when, but an event-driven program must be able to respond to input at unpredictable moments. As we shall see, the easiest way to do this is to use some of Python's more advanced features.
Tkinter is one of several toolkits you can use to build GUis in Python, and other languages have toolkits of their own. However, knowing how to put buttons and sliders on the screen is only part of knowing how to create an application that people will understand, use, and enjoy. To do the latter, you will also need to know something about graphic design and other aspects of human-computer interaction. Hundreds of books have been written on the subject (we particularly like [Joh07]), and you can find hundreds of tutorials and resources on the Web.
Figure 14.1: A root window
Was this article helpful?