Up |
Next: Hellow-world summary
(See also Hello World summary.)
- 1: Every Tkinter program imports the Tkinter module. Usually,
programmers will import the contents of the entire module into the
local namespace with import *.
- 3: The application is modeled as a subclass of an
existing widget, in this case, a Frame, which is just an empty container. The
constructor for this class will create the widgets for this
application The methods of this class are the methods that make sense
for this application, and the callbacks to the various application
widgets in this case, just the "say hi" method.
- 7-19: A method, called by the constructor, to create all the
application widgets.
- 8: Here, we create a new Button, and we call the button QUIT. The
self parameter is passed into the Button constructor as the master of
the new button. Put simply, if widget M is the master of widget W,
then W is contained entirely inside M.
- 9: We give the new widget a label. Note how we are treating the
new widget as if it were a dictionary (see Python programming
manual). You can configure an object's attributes ("options" in Tk
parlance) by assigning to that object as if it was a dictionary, with
the attribute name given as a dictionary key. Legitemate keys are
discussed in the options section.
- 10: Again, setting an attribute, making the button's foreground
color red.
- 11: Another attribute setting. The "command" attribute points to
a function to call when the button is pressed. In this case, we call
the (inherited) self.quit method which will quit the program.
- 13: Pack the quit button as far to the left as possible inside
the button's master. This is a call to the geometry manager called the
Packer. There is a brief discussion about the
packer, with more information available in John Ousterhout's book.
- 15: Creating a new Button instance.
- 16: Setting the new button's label.
- 17: This button's command is an application method defined in
this class. It might have also been a regular function that takes no
arguments.
- 19: Pack this button as far to the left as possible, in this
case, up against the quit button.
- 22-25: The constructor for the application.
- 27: Create the application.
- 28: Start the application.