Entry & Leave Events


Event Mouse Entry and Leave

b1.bind('<Enter>',my_callback1)
b1.bind('<Leave>',my_callback2)
When Mouse enters or hovers over any widget we can trigger any call back function. Similarly when mouse moves out or Leave the widget we can trigger another call back function.

Tkinter Mouse Enter & Leave events to trigger function to manage the attributes using config()

Using config()

To change any attribute of the widget with the event we are using config() method. Several properties like text, font, background color etc can be changed inside the call back function.

Call back function

Here is one call back function to change the background color and text of a Button and a Label.
def my_callback1(event):
    b1.config(text='Welcome',bg='lightgreen')
    l2.config(text='Click to register',bg='lightyellow')
Above call back function is triggered when mouse enters over the Button ( b1 )
b1.bind('<Enter>',my_callback1)
Full code is here
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("600x300") 

def my_callback1(event):
    b1.config(text='Welcome',bg='green')
    l2.config(text='click this')
def my_callback2(event):
    b1.config(text='Thanks ',bg='gray')
    l2.config(text='You left the button')

def my_callback3(event):
    l1.config(text='Welcome')
def my_callback4(event):
    l1.config(text='Thanks ',bg='lightblue')    

b1=tk.Button(my_w,text='I am a Button',font=22)
b1.grid(row=0,column=0,padx=10,pady=10)
str1=tk.StringVar(value='I am entry ')
e1=tk.Entry(my_w,text=str1,font=22,bg='yellow')
e1.grid(row=1,column=0,padx=10,pady=10)

l1=tk.Label(my_w,text='I am Label',font=22,bg='lightgreen')
l1.grid(row=2,column=0,padx=10,pady=10)

l2=tk.Label(my_w,text='Message area',font=22,bg='yellow')
l2.grid(row=3,column=0,padx=10,pady=10)

b1.bind('<Enter>',my_callback1) # Mouse enters the button
b1.bind('<Leave>',my_callback2) # Mouse leaves the button
e1.bind('<Enter>',lambda event:str1.set('Data here')) # entry
l1.bind('<Enter>',my_callback3) # Mouse enters the Label 
l1.bind('<Leave>',my_callback4) # Mouse leaves the Label 
my_w.mainloop()

Mouse enter & Leave event over a Label

Mouse entry and leave event on Tkiner Label to update attributes by using config()


We used one Label here
import tkinter as tk
my_w = tk.Tk() # Root window 
my_w.geometry("300x180")  # width and height of the window 

l1 = tk.Label(my_w,  text='plus2net', width=15,font=26,bg='yellow' ) 
l1.grid(row=1,column=1,padx=70,pady=50) 

l1.bind('<Enter>',lambda e:l1.config(text='Welcome',bg='lightgreen'))
l1.bind('<Leave>',lambda e:l1.config(text='Thanks',bg='yellow'))

my_w.mainloop() # Keep the window open 
Moving elements using arrow keys. Tkinter Canvas Animation using Rectangles & Circles
Sin & cos curves in Canvas More Projects using Tkinter
Events
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer