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_entry.py def main(): app = Tkinter.Frame() app.grid() make_ui(app) app.mainloop() # "make_ui" is similar to the one in tkinter_label_entry2.py. # In addition to echoing when a button is pressed, we also # do it when the user hits the key. # # To get our function "echo_event" called, we need to link # it to the "" (return key pressed) action using # the "bind" method of the entry widget. This call only # binds the action for the one widget; if the user # presses in any other widget, "echo_event" will # not be called. Note that echo_cb and echo_event also # have differing number of arguments. "echo_cb" is a # callback, so it has no arguments (other than the ones # we define for our data). "echo_event" is an "event # handler", so its one argument is an event instance # (again, other than the ones we define). def make_ui(app): def quit_cb(app=app): app.quit() f1 = Tkinter.Frame(app) f1.grid(row=0, column=0, sticky="nsew") label = Tkinter.Label(f1, text="Prompt") label.grid(row=0, column=0) entry = Tkinter.Entry(f1) entry.grid(row=0, column=1) def echo_cb(entry=entry, label=label): label.config(text=entry.get()) def echo_event(event, entry=entry, label=label): echo_cb(entry, label) entry.bind("", echo_event) f2 = Tkinter.Frame(app) f2.grid(row=1, column=0, sticky="nsew") button = Tkinter.Button(f2, text="Echo", command=echo_cb) button.grid(row=0, column=0) button = Tkinter.Button(f2, text="Quit", command=quit_cb) button.grid(row=0, column=1) if __name__ == "__main__": main() print("Finished")