from __future__ import print_function # # Create a GUI with a text entry field. # try: import Tkinter except ImportError: import tkinter as Tkinter # We use the same pattern as tkinter_label.py def main(): app = Tkinter.Frame() app.grid() make_ui(app) app.mainloop() # "make_ui" is the almost the same as in tkinter_label.py. # Here, we create an "entry" widget after creating the # label widget (but before the buttons). We now arrange # the four widgets into a 2x2 grid, with the first row # being the label and entry, and the second row the two # buttons. (Notice that when you run the program, the # buttons are not evenly spaced.) # # The entry widget does not have a callback function, # so we change our buttons so that one still quits, but # the other prints the contents of the entry widget out # on "standard output", usually the terminal window # where we invoked this program. def make_ui(app): def quit_cb(app=app): app.quit() label = Tkinter.Label(app, text="Prompt") label.grid(row=0, column=0) entry = Tkinter.Entry(app) entry.grid(row=0, column=1) def echo_cb(entry=entry): print("Entry contains '%s'" % entry.get()) button = Tkinter.Button(app, text="Echo", command=echo_cb) button.grid(row=1, column=0) button = Tkinter.Button(app, text="Quit", command=quit_cb) button.grid(row=1, column=1) if __name__ == "__main__": main() print("Finished")