<Button-1> | Mouse Left button click |
<Button-2> | Mouse center button click |
<Button-3> | Mouse Right button click |
<B1-Motion> | Mouse Left button Press and move |
<ButtonRelease-1> | Mouse Left button release, ButtonRelease-2 for Middle and 3 for right button. |
<Double-Button-1> | Left Mouse key is double clicked. |
<Enter> | Mouse Entry over a widget |
<Leave> | Mouse Leave a widget |
<MouseWheel> | Mouse Wheel Up or Down rotation |
def my_callback(event):
l1.config(text='Clicked at : '+ str(event.x) +","+ str(event.y))
my_w.bind('<Button-1>',my_callback) #Mouse Left button click
This is same as using <ButtonPress-1> <1> to bind Mouse left button click.
l1.bind('<Enter>',lambda e:l1.config(text='Welcome'))
Full code with all mouse events are here
import tkinter as tk # Python 3
my_w = tk.Tk()
my_w.geometry("615x400")
def my_callback(event):
l1.config(text='Clicked at : '+ str(event.x) +","+ str(event.y))
l1=tk.Label(my_w,text='to Display',bg='yellow',font=('Times',26,'normal'))
l1.grid(row=0,column=1,padx=10,pady=10)
my_w.bind('<Button-1>',my_callback) # Mouse Left button click <ButtonPress-1><1>
#my_w.bind('<Button-2>',my_callback) # Mouse middle button click
#my_w.bind('<Button-3>',my_callback) # Mouse right button click
#my_w.bind('<B1-Motion>',my_callback) # Mouse left button pressed move
#my_w.bind('<ButtonRelease-1>',my_callback) # Mouse left button release
#my_w.bind('<Double-Button-1>',my_callback) # Mouse left button Double click
#l1.bind('<Enter>',lambda e:l1.config(text='Welcome')) # Mouse enters label
#l1.bind('<Leave>',lambda e:l1.config(text='Thanks')) # Mouse leaves label
#my_w.bind('<MouseWheel>',my_callback) # Mouse middle button click
my_w.mainloop()
<Key> | Any Key is pressed |
<Return> | Enter Key is pressed |
<KeyPress> | Key Press event of any widget |
<KeyRelease> | Key Release event of any widget |
<FocusIn> | When widget got focus |
<FocusOut> | When widget lost focus |
<Right> | When Right arrow is pressed |
<Left> | When Left arrow is pressed |
<Up> | When Up arrow is pressed |
<Down> | When Down arrow is pressed |
def my_callback(event):
l1.config(text='You Pressed : '+ event.char)
Binding the event
my_w.bind("<Key>", my_callback)
Using one-liner for Enter Key
my_w.bind("<Return>", lambda e:l1.config(text='Hi'))
import tkinter as tk # Python 3
my_w = tk.Tk()
my_w.geometry("615x400")
def my_callback(event):
l1.config(text='Clicked the key : '+ event.char)
l1=tk.Label(my_w,text='to Display',bg='yellow',font=('Times',26,'normal'))
l1.grid(row=0,column=1,padx=10,pady=10)
my_w.bind('<a>',my_callback) # Key a is pressed
my_w.bind('<K>',my_callback) # Key K is pressed
my_w.mainloop()
def my_t1(*args):
if(len(t1.get())>5):
t1.config(bg='red')
else:
t1.config(bg='lightgreen')
Full code is here
import tkinter as tk # Python 3
my_w = tk.Tk()
my_w.geometry("615x400")
font=('Times',26,'normal')
def my_callback(event):
l1.config(text='You Pressed : '+ event.char)
def my_t1(*args):
if(len(t1.get())>5):
t1.config(bg='red')
else:
t1.config(bg='lightgreen')
l1=tk.Label(my_w,text='to Display',bg='yellow',font=font)
l1.grid(row=0,column=0,padx=10,pady=10)
t1=tk.Entry(my_w,bg='lightgreen',font=font)
t1.grid(row=1,column=0,padx=10)
t2=tk.Entry(my_w,bg='lightgreen',font=font)
t2.grid(row=2,column=0,padx=10,pady=10)
my_w.bind("<Key>", my_callback) # Any key is pressed
my_w.bind("<Return>", lambda e:l1.config(text='Hi')) # Enter Key
t1.bind("<KeyPress>",my_t1)
#t1.bind("<KeyRelease>",my_t1)
t1.bind("<FocusIn>",lambda e:l1.config(text='Welcome'))
t1.bind("<FocusOut>",lambda e:l1.config(text='Thanks'))
my_w.mainloop()
my_w.bind('<Right>',right)
my_w.bind('<Left>',left)
my_w.bind('<Up>',up)
my_w.bind('<Down>',down)
Triggering on Right arrow key press
my_w.bind("<Right>",lambda e:l1.config(text='Right arrow'))
import tkinter as tk
# Function to open a new Tkinter window
def open_window(event=None):
new_window = tk.Toplevel(root)
new_window.title("New Window")
new_window.geometry("300x200")
label = tk.Label(new_window, text="This is a new window!", font=("Arial", 14), fg="#dc3545")
label.pack(pady=20)
# Main Tkinter window
root = tk.Tk()
root.title("Shortcut Key Example")
root.geometry("400x300")
# Label for instructions with Bootstrap 'text-primary' color
instructions = tk.Label(root, text="Press Ctrl+1 to open a new window", font=("Arial", 12), fg="#007bff")
instructions.pack(pady=50)
# Bind Ctrl+1 to the open_window function
root.bind("<Control-Key-1>", open_window)
# Run the Tkinter event loop
root.mainloop()
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.