# -*- coding: utf-8 -*- import tkinter as tk root = tk.Tk() root.title("tk8.py") root.geometry("200x200") ##################################### def bClick1(event): # Click mouse left button lb.config(text = 'Click 1 once') root.config(bg = 'red') def bClick2(event): # Double click button-1 print("Double click 1") def bRelease(event): # Release Button 1 lb.config(text = "Just release 1") root.config(bg = 'grey') def mEnter(event): # Mouse enter widget lb.config(text = "Mouse Entered.") def mLeave(event): # Mouse leasve widget lb.config(text = "Mouse leaved.") def focusin(event): # focused to widget lb.config(text = "Focused in button") def focusout(event): # focused out widget lb.config(text = "Focused out") def pressEnter(event): # press enter print("Enter pressed.") def pressKey(event): # press a key lb.config(text = event.keysym) # use event.char for single-character def shiftup(event): # press shift+up # can also used in Alt and Control print("Press Shift+Up") def configure(event): #size changed print(root.winfo_width(), " ", root.winfo_height() ) def motion(event): # mouse motion print(root.winfo_pointerxy()) #print(event.x,' ',event.y) btn = tk.Button(root, text = "Button1", \ borderwidth = 10, bg = '#0000ff', \ fg = 'yellow') btn2 = tk.Button(root, text = "Button2",\ borderwidth = 10, bg = '#0000ff', \ fg = 'yellow') lb = tk.Label(root, text = 'A Label', fg = 'darkgreen') btn.pack() btn2.pack() btn.bind('', bClick1) btn.bind('', bClick2) btn.bind('', bRelease) btn.bind('', mEnter) btn.bind('', mLeave) btn.bind('',focusin) #press tab btn.bind('',focusout) #btn.bind('',pressEnter) # have to focus on button first root.bind('',pressEnter) # work in window anywhere #Can work on many other keys such as #Tab,Cancel, F1-F12, Num-Lock,Next,End,Left,Up, Home, and so forth root.bind('',pressKey) root.bind("",shiftup) root.bind("",configure) root.bind("",motion) lb.pack() ##################################### root.mainloop()