def __init__(self, parent, title, width, height): """ Arguments: *parent* Parent Tk window or Null for none. *title* Title for window. *width, height* Dimensions of window. """ display = os.environ.get('DISPLAY', None) if not display: os.environ['DISPLAY'] = ':0' Tk.__init__(self, parent) def mouseclick_callback(event): if not self.resized: self.ev = 'click' self.x = event.x self.y = event.y def mousemove_callback(event): if not self.resized: self.ev = 'move' self.x = event.x self.y = event.y def mousewheel_callback(event): if not self.resized: self.ev = 'wheel' self.num = event.num self.delta = event.delta def drag_callback(event): if not self.resized: self.ev = 'drag' self.x = event.x self.y = event.y mouserot = event.x def resize_callback(event): self.ev = 'resized' self.winx = self.winfo_x() self.winy = self.winfo_y() self.width = event.width self.height = event.height self.resized = True def key_callback(event): if not self.resized: self.ev = 'key' self.key = event.keysym self.char = event.char Tk.bind(self, '<Button-1>', mouseclick_callback) Tk.bind(self, '<B1-Motion>', drag_callback) Tk.bind(self, '<Motion>', mousemove_callback) Tk.bind(self, '<MouseWheel>', mousewheel_callback) Tk.bind(self, '<Configure>', resize_callback) Tk.bind_all(self, '<Key>', key_callback) Tk.geometry(self, str(width) + 'x' + str(height)) self.title(title) self.ev = '' self.resized = False
try: import tkMessageBox except: from tkinter import messagebox tkMessageBox = messagebox from no_wait_Dialog import no_wait_Dialog def disconnected(input=None): tkMessageBox.showerror( "Error", "The protocol failed; check your connection to the Arduino.") def warning(input=None): no_wait_Dialog( "Warning", "There was a problem in the connection. " "The connection has been reset and the valve states have been restored." ) root = Tk() try: root.iconbitmap('KATARA.ico') except: pass config.root = root root.bind_all("<<connection_warning>>", warning) root.bind_all("<<disconnected_error>>", disconnected) app = KATARAGUI(root) root.mainloop()
from Tkinter import Tk, Label root = Tk() prompt = ' Press any key ' label1 = Label(root, text=prompt, width=len(prompt), bg='yellow') label1.pack() def key(event): if event.char != event.keysym: msg = 'Special Key %r' % event.keysym label1.config(text=msg) else: print "WALA!" root.bind_all('<Key>', key) root.mainloop()