在Python中使用鼠标光标时显示消息 [英] Display message when going over something with mouse cursor in Python

查看:1651
本文介绍了在Python中使用鼠标光标时显示消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个gui用Python的TKinter。我想在鼠标光标移动时显示一条消息,例如,在标签或按钮的顶部。这样做的目的是向用户解释按钮/标签的作用或代表。有没有办法在Python中这样做?

I have a gui made with TKinter in Python. I would like to be able to display a message when my mouse cursor goes, for example, on top of a label or button. The purpose of this is to explain to the user what the button/label does or represents. Is there a way to do that in Python?

推荐答案

您需要在 ;输入> < Leave>

注意:弹出一个窗口(即:一个工具提示),确保你不要直接在鼠标下弹出它。会发生什么,它会导致一个离开事件触发,因为光标离开标签并进入弹出窗口。然后,您的离开处理程序将关闭窗口,您的光标将输入标签,这将导致一个enter事件,弹出窗口,这会导致离开事件,这会导致一个enter事件,...广告无限。

Note: if you choose to pop up a window (ie: a tooltip) make sure you don't pop it up directly under the mouse. What will happen is that it will cause a leave event to fire because the cursor leaves the label and enters the popup. Then, your leave handler will dismiss the window, your cursor will enter the label, which causes an enter event, which pops up the window, which causes a leave event, which dismisses the window, which causes an enter event, ... ad infinitum.

这里有一个仅更新标签的示例,类似于某些应用程序使用的状态栏。

Here's an example that merely updates a label, similar to a statusbar that some apps use.

import Tkinter as tk

class Example(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        self.l1 = tk.Label(self, text="Hover over me")
        self.l2 = tk.Label(self, text="", width=40)
        self.l1.pack(side="top")
        self.l2.pack(side="top", fill="x")

        self.l1.bind("<Enter>", self.on_enter)
        self.l1.bind("<Leave>", self.on_leave)

    def on_enter(self, event):
        self.l2.configure(text="Hello world")

    def on_leave(self, enter):
        self.l2.configure(text="")

if __name__ == "__main__":
    root = tk.Tk()
    Example(root).pack(side="top", fill="both", expand="true")
    root.mainloop()

这篇关于在Python中使用鼠标光标时显示消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆