from __future__ import print_function # # Create a GUI with a two buttons. # One (quit) causes the event loop to terminate. # The other (die) causes the program to exit. # try: import Tkinter except ImportError: import tkinter as Tkinter # Our program is pretty generic and can be used in many # GUI programs. Templates like these are also known as # "patterns". The pattern in this case is to (1) create # a top-level application, (2) create the user interface # elements within, and (3) enter the event loop. def main(): app = Tkinter.Frame(bg="red") app.grid(sticky="nsew") make_ui(app) app.mainloop() # make_ui creates the widgets inside a top-level application # frame. Here, we define two functions; we then define two # buttons, each using one of the functions as callback. # Note that we place them one on top of the other by specifying # their row positions in a grid. def make_ui(app): def quit_cb(app=app): app.quit() def die(): raise SystemExit(1) button = Tkinter.Button(app, text="Quit", command=quit_cb) button.grid(row=0, column=0, sticky="nsew") button = Tkinter.Button(app, text="Die", command=die) button.grid(row=1, column=0, sticky="nsew") if __name__ == "__main__": main() print("Finished")