如何在Tkinter Listbox的光标旁显示双击打印提示文本?
Great question! Tkinter’s Listbox doesn’t come with a built-in cursor-side text prompt feature, but we can absolutely build this ourselves with a couple of straightforward approaches. Let’s walk through two reliable solutions:
If you want to avoid third-party dependencies, you can create a lightweight, borderless tooltip window that follows the mouse cursor over the Listbox. Here’s how:
- Create a hidden
Toplevelwindow to act as the tooltip (we’ll remove its title bar and borders for a clean look). - Bind the Listbox’s
<Motion>event to update the tooltip’s position and show it whenever the mouse moves over the Listbox. - Bind the
<Leave>event to hide the tooltip when the mouse exits the Listbox. - Keep your existing double-click functionality intact.
Here’s a complete working example:
import tkinter as tk def on_listbox_double_click(event): # 原有双击打印逻辑 selected_item = event.widget.get(event.widget.curselection()) print(selected_item) def update_tooltip_position(event): # 显示提示框并更新位置(偏移光标一点避免遮挡) tooltip_window.deiconify() x_pos = event.x_root + 15 y_pos = event.y_root + 15 tooltip_window.geometry(f"+{x_pos}+{y_pos}") def hide_tooltip(event): # 鼠标离开时隐藏提示框 tooltip_window.withdraw() # 主窗口设置 root = tk.Tk() root.title("Listbox Cursor Tip Demo") # 创建Listbox并添加示例内容 listbox = tk.Listbox(root, width=30, height=10) listbox.pack(padx=10, pady=10) for item in ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5"]: listbox.insert(tk.END, item) # 绑定双击事件 listbox.bind("<Double-1>", on_listbox_double_click) # 创建提示框窗口 tooltip_window = tk.Toplevel(root) tooltip_window.overrideredirect(True) # 去掉窗口边框和标题栏 tooltip_window.attributes("-topmost", True) # 保持提示框在最上层 # 添加提示文本标签 tooltip_label = tk.Label( tooltip_window, text="双击选中以打印内容", bg="#ffffe0", # 浅黄色背景,类似默认tooltip relief="solid", borderwidth=1, padx=3, pady=2 ) tooltip_label.pack() # 初始隐藏提示框 tooltip_window.withdraw() # 绑定鼠标事件 listbox.bind("<Motion>", update_tooltip_position) listbox.bind("<Leave>", hide_tooltip) root.mainloop()
This implementation gives you full control over the tooltip’s appearance and behavior—you can tweak the background color, text style, or position offset to match your app’s design.
If you prefer a more polished, low-code solution, the tktooltip library is purpose-built for this kind of task. It handles cursor following, tooltip delays, and styling out of the box.
First, install the library:
pip install tktooltip
Then use it with your Listbox like this:
import tkinter as tk from tktooltip import ToolTip def on_listbox_double_click(event): selected_item = event.widget.get(event.widget.curselection()) print(selected_item) root = tk.Tk() root.title("Listbox with tktooltip") listbox = tk.Listbox(root, width=30, height=10) listbox.pack(padx=10, pady=10) for item in ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5"]: listbox.insert(tk.END, item) listbox.bind("<Double-1>", on_listbox_double_click) # 创建跟随光标的提示框 ToolTip( listbox, msg="双击选中以打印内容", follow=True, # 开启光标跟随 delay=0, # 鼠标悬停后立即显示(可调整为毫秒值延迟) bg="#ffffe0" ) root.mainloop()
This is much cleaner and requires less boilerplate code. The library also supports additional features like customizing the tooltip’s font, padding, and display delay.
内容的提问来源于stack exchange,提问作者AD WAN

