# -*- coding: utf-8 -*- # Python 2 & 3 from __future__ import print_function # Replace print function import time import threading import locale import sys try: # { v2 import Tkinter as tkinter import ttk # Tk themed widgets print = lambda text: sys.stdout.write(text.decode('utf-8', errors='replace').encode( locale.getlocale()[1], errors='replace' ) + '\n') except: # v3 import tkinter from tkinter import ttk # Tk themed widgets # } class Application(object): # { def __init__(self, init_root): # { self.root = init_root self.label_var = tkinter.StringVar() ttk.Label(self.root, textvariable=self.label_var).pack() self.label_var.set("Press any key, Enter, Escape") self.button_exit = ttk.Button(self.root, command=self.callback_exit, text="Exit") self.button_exit.pack() self.root.bind('', self.callback_bind_escape) # Escape key self.root.bind('', self.callback_bind_key) # any key self.root.bind('', self.callback_bind_return) # Enter key self.root.update() #self.button_exit.focus() # Select as default/selected button # } def callback_bind_escape(self, event=None, *args): # { self.button_exit.invoke() # Execute button # } def callback_bind_key(self, event=None, *args): # { if event: # { print(event.char) if len(event.char): # printable self.label_var.set(repr(event.char)) # Encoding for special characters is required # } # } def callback_bind_return(self, event=None, *args): # { if event.widget is self.button_exit: # { If button_exit focused/selected self.button_exit.invoke() # Execute button else: print("Focus on Exit button to Exit, with tab") self.label_var.set("Focus on Exit button to Exit, with tab") # } # } def callback_exit(self): # { self.root.destroy() print("Exit") self.label_var.set("Exit") # } # } class if __name__ == "__main__": # { root = tkinter.Tk() application = Application(root) root.mainloop() # Blocking until destroy() print("Done") # }