python tkinter實現(xiàn)鼠標(biāo)懸停提示
展示效果

全部代碼和使用示例
# _*_ coding:utf-8 _*_
import tkinter as tk
import pyautogui
screen_width, screen_height = pyautogui.size()
class WidgetTip:
"""鼠標(biāo)懸停提示"""
def __init__(self, widget, text):
self.widget = widget
self.text = text
self.tips_alpha = 1
self.tooltip = tk.StringVar()
self.tip_pos = 5
# 消息體
self.tooltip_window = tk.Toplevel()
self.init_tips()
# 綁定鼠標(biāo)事件
self.widget.bind("<Enter>", self.enter)
self.widget.bind("<Motion>", self.move)
self.widget.bind("<Leave>", self.leave)
def init_tips(self):
"""初始化消息框"""
self.tooltip_window.attributes("-alpha", 0)
self.tooltip_window.attributes("-toolwindow", 1)
self.tooltip_window.wm_overrideredirect(True)
self.tooltip_window.attributes('-topmost', 100)
pad_x = 20 # 內(nèi)邊距 x
tk.Label(self.tooltip_window,
textvariable=self.tooltip,
background="#ffffff",
justify=tk.LEFT,
relief="solid",
borderwidth=0,
wraplength=screen_width - pad_x,
padx=pad_x, pady=10).pack()
def enter(self, event=None):
self.tooltip_window.lift()
if isinstance(self.text, (tk.StringVar, tk.IntVar, tk.BooleanVar, tk.DoubleVar)):
tips_text = str(self.text.get())
else:
tips_text = str(self.text)
self.tooltip.set(tips_text)
self.set_tooltip_window_geometry(event)
self.tooltip_window.attributes("-alpha", self.tips_alpha)
def move(self, event=None):
self.set_tooltip_window_geometry(event)
def leave(self, event=None):
self.tooltip_window.attributes("-alpha", 0)
self.set_tooltip_window_geometry(event)
def set_tooltip_window_geometry(self, event):
cost_length = event.x_root + self.tip_pos + self.tooltip_window.winfo_width() - screen_width
if cost_length < 0:
cost_length = 0
self.tooltip_window.geometry("+%d+%d" % (event.x_root + self.tip_pos - cost_length, event.y_root + self.tip_pos))
# 創(chuàng)建主窗口
root = tk.Tk()
root.title("鼠標(biāo)懸停提示")
var_str = tk.StringVar()
# 創(chuàng)建一個按鈕并添加 Tooltip
button = tk.Button(root, text="鼠標(biāo)放這")
button.pack(pady=20)
var_str.set('動態(tài)文本按鈕')
button2 = tk.Button(root, text=var_str.get())
button2.pack(pady=20)
var_str.set('這是一條動態(tài)文本提示語')
WidgetTip(button, "這是一條提示鼠標(biāo)懸停提示信息")
WidgetTip(button2, var_str)
# 運行主循環(huán)
root.mainloop()到此這篇關(guān)于python tkinter實現(xiàn)鼠標(biāo)懸停提示的文章就介紹到這了,更多相關(guān)python鼠標(biāo)懸停內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python數(shù)據(jù)結(jié)構(gòu)之順序表的實現(xiàn)代碼示例
這篇文章主要介紹了Python數(shù)據(jù)結(jié)構(gòu)之順序表的實現(xiàn)代碼示例,簡單介紹了順序表的相關(guān)內(nèi)容,然后分享了其代碼示例,具有一定參考價值,需要的朋友可以了解下。2017-11-11
python調(diào)用Elasticsearch執(zhí)行增刪改查操作
Elasticsearch 是一種強大且靈活的分布式搜索引擎,而 Python 則以其易用性和強大的數(shù)據(jù)處理能力,成為開發(fā)者在數(shù)據(jù)操作中的理想選擇,本文將介紹二者如何結(jié)合實現(xiàn)增刪改查操作,感興趣的可以了解下2025-04-04
python Tkinter實時顯示數(shù)據(jù)功能實現(xiàn)
這篇文章主要介紹了python Tkinter實時顯示數(shù)據(jù)功能實現(xiàn),本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07
pytorch交叉熵?fù)p失函數(shù)的weight參數(shù)的使用
這篇文章主要介紹了pytorch交叉熵?fù)p失函數(shù)的weight參數(shù)的使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-05-05
基于循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)的古詩生成器
這篇文章主要為大家詳細(xì)介紹了基于循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)的古詩生成器,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-03-03
使用Python實現(xiàn)在Windows下安裝Django
今天小編就為大家分享一篇關(guān)于使用Python實現(xiàn)在Windows下安裝Django,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-10-10
Python numpy有哪些常用數(shù)據(jù)類型
Numpy提供了兩種基本的對象:ndarray(N-dimensional Array Object)和 ufunc(Universal Function Object)。ndarray是存儲單一數(shù)據(jù)類型的多維數(shù)組,而ufunc則是能夠?qū)?shù)組進行處理的函數(shù)2023-02-02

