pythonware.com | products ::: library ::: search ::: daily Python-URL! |
Hello, AgainChapter 3. Hello, Again
When you write larger programs, it is usually a good idea to wrap your code up in one or more classes. The following example is adapted from the "hello world" program in Matt Conway's A Tkinter Life Preserver. Example 3-1. Our Second Tkinter Program # File: hello2.py from Tkinter import * class App: def __init__(self, master): frame = Frame(master) frame.pack() self.button = Button(frame, text="QUIT", fg="red", command=frame.quit) self.button.pack(side=LEFT) self.hi_there = Button(frame, text="Hello", command=self.say_hi) self.hi_there.pack(side=LEFT) def say_hi(self): print "hi there, everyone!" root = Tk() app = App(root) root.mainloop() Running the ExampleWhen you run this example, the following window appears. Figure 3-1. Running the sample program (using Tk 8.0 on a Windows 95 box) If you click the right button, the text "hi there, everyone!" is printed to the console. If you click the left button, the program stops. |