Next / Previous / Contents / TCC Help System / NM Tech homepage

2. A minimal application

Here is a trivial Tkinter program containing only a Quit button:

#!/usr/local/bin/python     1
from Tkinter import *       2

class Application(Frame):              3
    def __init__(self, master=None):
        Frame.__init__(self, master)   4
        self.grid()                    5
        self.createWidgets()

    def createWidgets(self):
        self.quitButton = Button ( self, text="Quit",
            command=self.quit )        6
        self.quitButton.grid()         7

app = Application()                    8
app.master.title("Sample application") 9
app.mainloop()                         10
1 This line makes the script self-executing, assuming that your system has the Python interpreter at path /usr/local/bin/python.
2 This line imports the entire Tkinter package into your program's namespace.
3 Your application class must inherit from Tkinter's Frame class.
4 Calls the constructor for the parent class, Frame.
5 Necessary to make the application actually appear on the screen.
6 Creates a button labeled “Quit”.
7 Places the button on the application.
8 The main program starts here by instantiating the Application class.
9 This method call sets the title of the window to “Sample application”.
10 Starts the application's main loop, waiting for mouse and keyboard events.