Home > OS >  Tkinter - How to understand when the cursor is placed over a text widget?
Tkinter - How to understand when the cursor is placed over a text widget?

Time:10-03

I would very like to do something when the mouse cursor is placed over a TK Text widget (without clicking). How can I get this event?

CodePudding user response:

Tkinter fires the <Enter> event with the cursor enters a widget, and <Leave> when it leaves.

import tkinter as tk

def handle_enter(event):
    event.widget.insert("end", "Cursor has entered the chat\n")

def handle_leave(event):
    event.widget.insert("end", "Cursor has left the chat\n")

root = tk.Tk()
text = tk.Text(root)
text.pack(fill="both", expand=True)

text.bind("<Enter>", handle_enter)
text.bind("<Leave>", handle_leave)

root.mainloop()

CodePudding user response:

Try the following code. It is a quick snippet:

#Import the tkinter library
from tkinter import *
from tkinter.tix import *

#Create an instance of tkinter frame
win = Tk()
#Set the geometry
win.geometry("400x200")

#Create a tooltip
tip= Balloon(win)

#Create a Button widget
my_button=Button(win, text= "Python", font=('Poppins', 20))
my_button.pack(pady=20)

#Bind the tooltip with button
tip.bind_widget(my_button,balloonmsg="Python is an interpreted, high-level
and general-purpose programming language")

win.mainloop()
  • Related