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()
相关推荐
A__tao10 小时前
Elasticsearch Mapping 一键生成 Java 实体类(支持嵌套 + 自动过滤注释)
java·python·elasticsearch
研究点啥好呢10 小时前
Github热门项目推荐 | 创建你的像素风格!
c++·python·node.js·github·开源软件
lly20240610 小时前
C 标准库 - `<stdio.h>`
开发语言
沫璃染墨10 小时前
C++ string 从入门到精通:构造、迭代器、容量接口全解析
c语言·开发语言·c++
jwn99910 小时前
Laravel6.x核心特性全解析
开发语言·php·laravel
迷藏49410 小时前
**发散创新:基于Rust实现的开源合规权限管理框架设计与实践**在现代软件架构中,**权限控制(RBAC)** 已成为保障
java·开发语言·python·rust·开源
功德+n11 小时前
Linux下安装与配置Docker完整详细步骤
linux·运维·服务器·开发语言·docker·centos
明日清晨11 小时前
python扫码登录dy
开发语言·python
我是唐青枫11 小时前
C#.NET gRPC 深入解析:Proto 定义、流式调用与服务间通信取舍
开发语言·c#·.net
JJay.11 小时前
Kotlin 高阶函数学习指南
android·开发语言·kotlin