Tkinter Button bind hover message

This is the hover message (tooltip-style) behavior into a reusable class is the right way to go.

python 复制代码
import tkinter as tk

class HoverTooltip:
    """A reusable tooltip class for Tkinter widgets."""

    def __init__(self, widget, text, offset=(10, 10)):
        self.widget = widget
        self.text = text
        self.offset = offset
        self.tooltip = None

        # Bind hover events
        widget.bind("<Enter>", self.show_tooltip)
        widget.bind("<Leave>", self.hide_tooltip)
        widget.bind("<Motion>", self.move_tooltip)

    def show_tooltip(self, event):
        """Display the tooltip near the cursor."""
        if self.tooltip or not self.text:
            return

        self.tooltip = tk.Toplevel(self.widget)
        self.tooltip.wm_overrideredirect(True)  # no window borders
        self.tooltip.attributes("-topmost", True)

        label = tk.Label(
            self.tooltip,
            text=self.text,
            bg="yellow",
            relief="solid",
            borderwidth=1,
            padx=5,
            pady=2,
        )
        label.pack(ipadx=1)

        # Position the tooltip initially
        self.move_tooltip(event)

    def move_tooltip(self, event):
        """Update tooltip position as the cursor moves."""
        if self.tooltip:
            x = event.x_root + self.offset[0]
            y = event.y_root + self.offset[1]
            self.tooltip.geometry(f"+{x}+{y}")

    def hide_tooltip(self, event):
        """Destroy the tooltip when cursor leaves."""
        if self.tooltip:
            self.tooltip.destroy()
            self.tooltip = None


# --- Example usage ---
if __name__ == "__main__":
    root = tk.Tk()
    root.title("Hover Tooltip Class Example")

    btn1 = tk.Button(root, text="Hover Me 1")
    btn1.pack(pady=10)

    btn2 = tk.Button(root, text="Hover Me 2")
    btn2.pack(pady=10)

    HoverTooltip(btn1, "This is the first button tooltip!")
    HoverTooltip(btn2, "Another helpful message here!")

    root.mainloop()

Another simple way

python 复制代码
# 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=('Helvetica bold', 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()
相关推荐
qq_336313932 小时前
javaweb-web基础(springboot入门)
java·开发语言·mysql
玄同7652 小时前
LangChain 1.0 模型接口:多厂商集成与统一调用
开发语言·人工智能·python·langchain·知识图谱·rag·智能体
特立独行的猫a2 小时前
C++轻量级Web框架介绍与对比:Crow与httplib
开发语言·前端·c++·crow·httplib
fie88892 小时前
基于C#的推箱子小游戏实现
开发语言·c#
喵手2 小时前
Python爬虫实战:构建招聘会数据采集系统 - requests+lxml 实战企业名单爬取与智能分析!
爬虫·python·爬虫实战·requests·lxml·零基础python爬虫教学·招聘会数据采集
菜鸟小芯2 小时前
Qt Creator 集成开发环境下载安装
开发语言·qt
阿猿收手吧!2 小时前
【C++】引用类型全解析:左值、右值与万能引用
开发语言·c++
「QT(C++)开发工程师」3 小时前
C++ 策略模式
开发语言·c++·策略模式
专注VB编程开发20年3 小时前
python图片验证码识别selenium爬虫--超级鹰实现自动登录,滑块,点击
数据库·python·mysql