
my_w.bind('<Key>',my_callback) # Any Key is pressed
Here inside the function first we will check which key is pressed by using event.char. The same we will use to update the Label l1 to print the char associated with the Key.
l1.config(text=event.char) # Update the text on Label
We can get all the widget classes of the application by using winfo_children(). We can loop through the list of widgets and check for matching button class. def my_callback(event): # When any key is pressed
l1.config(text=event.char) # Update the text on Label
for widget in my_w.winfo_children(): # Collect all classes of the widgets
if isinstance(widget, tk.Button): # If it belongs to button class
if widget['text']==event.char or widget['text'].lower()==event.char:
widget['relief']='sunken' # Update relief option to pressed
my_w.bind("<KeyRelease>",my_release)
Inside our function my_release() we will have update the relief attribute of all buttons to raised. Here also we will list all widgets of the application by using winfo_children().
def my_release(event):
for widget in my_w.winfo_children(): # all widget classes of the application
if isinstance(widget, tk.Button): # if belongs to button class
widget['relief']='raised' # Update the relief option to raised
Full code is here
import tkinter as tk # Python 3
my_w = tk.Tk()
my_w.geometry("475x200")
def my_callback(event): # When any key is pressed
l1.config(text=event.char) # Update the text on Label
for widget in my_w.winfo_children(): # Collect all classes of the widgets
if isinstance(widget, tk.Button): # If it belongs to button class
if widget['text']==event.char or widget['text'].lower()==event.char:
widget['relief']='sunken' # Update relief option to pressed
def my_release(event):
for widget in my_w.winfo_children(): # all widget classes of the application
if isinstance(widget, tk.Button): # if belongs to button class
widget['relief']='raised' # Update the relief option to raised
ba=tk.Button(my_w,text='A',font=28,width=2)
ba.grid(row=0,column=0,padx=10,pady=20)
bs=tk.Button(my_w,text='S',font=28,width=2)
bs.grid(row=0,column=1,padx=10,pady=10)
bd=tk.Button(my_w,text='D',font=28,width=2)
bd.grid(row=0,column=2,padx=10,pady=10)
bf=tk.Button(my_w,text='F',font=28,width=2)
bf.grid(row=0,column=3,padx=10,pady=10)
bg=tk.Button(my_w,text='G',font=28,width=2)
bg.grid(row=0,column=4,padx=10,pady=10)
bh=tk.Button(my_w,text='H',font=28,width=2)
bh.grid(row=0,column=5,padx=10,pady=10)
bj=tk.Button(my_w,text='J',font=28,width=2)
bj.grid(row=0,column=6,padx=10,pady=10)
bk=tk.Button(my_w,text='K',font=28,width=2)
bk.grid(row=0,column=7,padx=10,pady=10)
bl=tk.Button(my_w,text='L',font=28,width=2)
bl.grid(row=0,column=8,padx=10,pady=10)
l1=tk.Label(my_w,text='#',bg='yellow',width=2,font=('Times',26,'normal'))
l1.grid(row=1,column=0,padx=2,pady=10,columnspan=3)
my_w.bind('<Key>',my_callback) # Any Key is pressed
#my_w.bind('<s>',my_callback) # Key s is pressed
my_w.bind("<KeyRelease>",my_release)
my_w.mainloop()

bk=tk.Button(my_w,text='K',font=28,width=2,command=lambda:my_click())
bk.grid(row=0,column=7,padx=10,pady=10)
We will add one function my_click()
def my_click():
print('Welcome to plus2net')
l2.config(text='plus2net.com')
Inside our function my_callback() we will add invoke()
def my_callback(event): # When any key is pressed
l1.config(text=event.char) # Update the text on Label
for widget in my_w.winfo_children(): # Collect all classes of the widgets
if isinstance(widget, tk.Button): # If it belongs to button class
if widget['text']==event.char or widget['text'].lower()==event.char:
widget['relief']='sunken' # Update relief option to pressed
widget.invoke()
import tkinter as tk # Python 3
my_w = tk.Tk()
my_w.geometry("475x200")
def my_callback(event): # When any key is pressed
l1.config(text=event.char) # Update the text on Label
for widget in my_w.winfo_children(): # Collect all classes of the widgets
if isinstance(widget, tk.Button): # If it belongs to button class
if widget['text']==event.char or widget['text'].lower()==event.char:
widget['relief']='sunken' # Update relief option to pressed
widget.invoke()
def my_release(event):
for widget in my_w.winfo_children(): # all widget classes of the application
if isinstance(widget, tk.Button): # if belongs to button class
widget['relief']='raised' # Update the relief option to raised
def my_click():
print('Welcome to plus2net')
l2.config(text='plus2net.com')
ba=tk.Button(my_w,text='A',font=28,width=2)
ba.grid(row=0,column=0,padx=10,pady=20)
bs=tk.Button(my_w,text='S',font=28,width=2)
bs.grid(row=0,column=1,padx=10,pady=10)
bd=tk.Button(my_w,text='D',font=28,width=2)
bd.grid(row=0,column=2,padx=10,pady=10)
bf=tk.Button(my_w,text='F',font=28,width=2)
bf.grid(row=0,column=3,padx=10,pady=10)
bg=tk.Button(my_w,text='G',font=28,width=2)
bg.grid(row=0,column=4,padx=10,pady=10)
bh=tk.Button(my_w,text='H',font=28,width=2)
bh.grid(row=0,column=5,padx=10,pady=10)
bj=tk.Button(my_w,text='J',font=28,width=2)
bj.grid(row=0,column=6,padx=10,pady=10)
bk=tk.Button(my_w,text='K',font=28,width=2,command=lambda:my_click())
bk.grid(row=0,column=7,padx=10,pady=10)
bl=tk.Button(my_w,text='L',font=28,width=2)
bl.grid(row=0,column=8,padx=10,pady=10)
l1=tk.Label(my_w,text='#',bg='yellow',width=2,font=('Times',26,'normal'))
l1.grid(row=1,column=0,padx=2,pady=10,columnspan=3)
l2=tk.Label(my_w,text='#',bg='lightyellow',font=('Times',26,'normal'))
l2.grid(row=2,column=0,padx=2,pady=10,columnspan=6)
my_w.bind('<Key>',my_callback) # Any Key is pressed
#my_w.bind('<s>',my_callback) # Key s is pressed
my_w.bind("<KeyRelease>",my_release)
my_w.mainloop()
Moving elements using arrow keys.
Tkinter Canvas Animation using Rectangles & Circles
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.