gpt4 book ai didi

python - 如何在 Tkinter 中显示工具提示?

转载 作者:IT老高 更新时间:2023-10-28 21:48:48 27 4
gpt4 key购买 nike

工具提示是当鼠标悬停在小部件上一段时间时弹出的那些小文本。

如何向我的 tkinter Python 应用程序添加工具提示消息?

Example of tooltip

最佳答案

我尝试了 blog post 中的代码由 ars 提及,并尝试了 IDLE lib 中的代码.

虽然两者都有效,但我不喜欢 IDLE 中的工具提示如何限制大小(必须手动输入新行作为单独的列表),以及提示如何立即出现在博客文章的代码中。

所以我在两者之间做了一个混合体。它允许您指定环绕长度和悬停时间,对每个都没有限制:

""" tk_ToolTip_class101.py
gives a Tkinter widget a tooltip as the mouse is above the widget
tested with Python27 and Python34 by vegaseat 09sep2014
www.daniweb.com/programming/software-development/code/484591/a-tooltip-class-for-tkinter

Modified to include a delay time by Victor Zaccardo, 25mar16
"""

try:
# for Python2
import Tkinter as tk
except ImportError:
# for Python3
import tkinter as tk

class CreateToolTip(object):
"""
create a tooltip for a given widget
"""
def __init__(self, widget, text='widget info'):
self.waittime = 500 #miliseconds
self.wraplength = 180 #pixels
self.widget = widget
self.text = text
self.widget.bind("<Enter>", self.enter)
self.widget.bind("<Leave>", self.leave)
self.widget.bind("<ButtonPress>", self.leave)
self.id = None
self.tw = None

def enter(self, event=None):
self.schedule()

def leave(self, event=None):
self.unschedule()
self.hidetip()

def schedule(self):
self.unschedule()
self.id = self.widget.after(self.waittime, self.showtip)

def unschedule(self):
id = self.id
self.id = None
if id:
self.widget.after_cancel(id)

def showtip(self, event=None):
x = y = 0
x, y, cx, cy = self.widget.bbox("insert")
x += self.widget.winfo_rootx() + 25
y += self.widget.winfo_rooty() + 20
# creates a toplevel window
self.tw = tk.Toplevel(self.widget)
# Leaves only the label and removes the app window
self.tw.wm_overrideredirect(True)
self.tw.wm_geometry("+%d+%d" % (x, y))
label = tk.Label(self.tw, text=self.text, justify='left',
background="#ffffff", relief='solid', borderwidth=1,
wraplength = self.wraplength)
label.pack(ipadx=1)

def hidetip(self):
tw = self.tw
self.tw= None
if tw:
tw.destroy()

# testing ...
if __name__ == '__main__':
root = tk.Tk()
btn1 = tk.Button(root, text="button 1")
btn1.pack(padx=10, pady=5)
button1_ttp = CreateToolTip(btn1, \
'Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, '
'consectetur, adipisci velit. Neque porro quisquam est qui dolorem ipsum '
'quia dolor sit amet, consectetur, adipisci velit. Neque porro quisquam '
'est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit.')

btn2 = tk.Button(root, text="button 2")
btn2.pack(padx=10, pady=5)
button2_ttp = CreateToolTip(btn2, \
"First thing's first, I'm the realest. Drop this and let the whole world "
"feel it. And I'm still in the Murda Bizness. I could hold you down, like "
"I'm givin' lessons in physics. You should want a bad Vic like this.")
root.mainloop()

截图:

Example of hovertext

关于python - 如何在 Tkinter 中显示工具提示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3221956/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com