PyTut: Tkinter #1

Rick Johnson rantingrickjohnson at gmail.com
Wed Feb 29 22:45:38 EST 2012


PyTut: Tkinter #1
Graciously donated by the benevolent professor Richard Johnson

The first step to creating a Tkinter GUI is to create a blank window.
Tkinter has two classes that represent a GUI window: Toplevel and Tk.
Both the classes are basically the same, but since a GUI can have an
unlimited number of Toplevel windows, the very first window should
ALWAYS be an instance of Tk! Let's write some code.

## START CODE ##
import Tkinter as tk

root = tk.Tk()
root.mainloop()
## END CODE ##

You can probably guess what the first line does; it creates your
parent Toplevel window as an instance of Tk! The second line
"root.mainloop()" enters the event loop. Don't worry about the details
of "mainloop" at this time, just remeber that all Tkinter GUIs start
with an instance of Tk and end with a call to mainloop.

Here is the same code in a strict OOP form (For something this simple
you need not use strict OOP but eventually you will need to harness
the power of OOP!)

## START CODE ##
class App(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)

app = App()
app.mainloop()
## END CODE ##

That's all folks! Keep your eyes peeled for the next glorious
installment of "PyTut: Tkinter"!



More information about the Python-list mailing list