Tkinter Tooltip over a text only in a text widget Tkinter Tooltip over a text only in a text widget tkinter tkinter

Tkinter Tooltip over a text only in a text widget


You can add tags to a span of characters in a text widget. You can then bind the mouse events <Enter> and <Leave. to those tags.

Here's a very contrived example:

import tkinter as tkdef show_info(text):    label.configure(text=text)root = tk.Tk()text_widget = tk.Text(root)label = tk.Label(root)label.pack(side="top", fill="x")text_widget.pack(fill="both", expand=True)for color in ("red", "orange", "yellow", "green", "blue", "indigo", "violet"):    tag = color    text = color    text_widget.insert("end", text+"\n", (tag, ))    text_widget.tag_configure(tag, background=color, foreground="white")    text_widget.tag_bind(tag, "<Enter>",                         lambda event, color=color: show_info(color))    text_widget.tag_bind(tag, "<Leave>",                         lambda event, color=color: show_info(""))tk.mainloop()