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") # By default nothing is resizable in Tkinter. # To make something resizable, you have to change # its "weight" in the parent widget. For "app" its # parent widget is the Toplevel widget. top = app.winfo_toplevel() # Make the application resizable in both directions # by setting the weights of both row and column 0. top.rowconfigure(0, weight=1) top.columnconfigure(0, weight=1) 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") # Make column 0 and row 0 resizable, but leave row 1 fixed height app.columnconfigure(0, weight=1) app.rowconfigure(0, weight=1) if __name__ == "__main__": main() print("Finished")