简体   繁体   English

将光标置于python中的按钮上方时显示文本

[英]Display text when cursor is placed over button in python

How to display text in the window when the cursor is placed over the button. 将光标置于按钮上方时如何在窗口中显示文本。

I have below code, when put cursor over the button "Ok" it has to show text as "Check details filled before pressing Ok". 我有下面的代码,当将光标放在按钮“确定”上时,它必须显示文本为“在按确定之前检查详细信息”。

import Tkinter

class Example(Tkinter.Frame):
    def __init__(self, *args, **kwargs):
        Tkinter.Frame.__init__(self, *args, **kwargs)
        self.l1 = Tkinter.Label(self, text="Enter name")
        self.l2 = Tkinter.Label(self, text="", width=40)
        self.l1.pack(side="top")
        self.l2.pack(side="top", fill="x")

        self.b1 = Tkinter.Button(root, text="Ok")
        self.b1.bind("<Enter>", self.on_enter)
        self.b1.bind("<Leave>", self.on_leave)
        self.b1.pack()       

    def on_enter(self, event):
        self.l2.configure(text="Check details filled before pressing Ok")

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

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

The same code works fine if I write for display text when cursor is placed over label l1. 如果我将光标放在标签l1上以显示文本,则相同的代码可以正常工作。 Is there any other way to proceed for displaying when cursor placed on button or any modifications?? 将光标置于按钮上或进行任何修改时,还有其他方法可以继续显示吗?

As far as I know, Tk doesn't have a built in construct for button ToolTips 据我所知,Tk没有用于按钮工具提示的内置构造

In Qt (PyQt), which is another GUI framework, this aa built in feature - for example: 在另一个GUI框架Qt(PyQt)中,这是一个内置功能-例如:

button1 = QtGui.QPushButton("This is button1", self)
button1.setToolTip("You have moused over Button1)

There are some workarounds for adding this type of functionality to Tk but it may take you some time to directly implement them into your program 有一些解决方法可将这种类型的功能添加到Tk中,但可能需要一些时间才能将其直接实现到程序中

Essentially you create your own class ToolTip() and a function in your module to add a new ToolTip, addToolTip() 本质上,您可以创建自己的类ToolTip()和模块中的函数以添加新的ToolTip addToolTip()

Here are two references for doing this: ref 1 ref 2 以下是执行此操作的两个参考: ref 1 ref 2

Edit: Note that ref1 here is the same link that is the accepted answer in the question Martineau links to in the comments for this question. 编辑:请注意,这里的ref1是与Martineau对此问题的评论中的问题所接受的答案相同的链接。

If I understand the question correctly, you want to be able to display different messages when the mouse is over different widgets. 如果我对问题的理解正确,那么当鼠标悬停在不同的小部件上时,您希望能够显示不同的消息。 The simplest solution is to add a custom attribute to each widget object that contains the message, and then get that message from the attribute in the bound function. 最简单的解决方案是向包含消息的每个窗口小部件对象添加一个自定义属性,然后从绑定函数的属性中获取该消息。

For example: 例如:

class Example(...):
    def __init__(...):
        ...
        self.l1.description = "This is label 1"
        self.l2.description = "This is label 2"
        self.b1.description = "This is the OK button"

        for widget in (self.l1, self.l2, self.b1):
            widget.bind("<Enter>", self.on_enter)
            widget.bind("<Leave>", self.on_leave)
        ...

    def on_enter(self, event):
        description = getattr(event.widget, "description", "")
        self.l2.configure(text=description)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Python 中用鼠标光标悬停在某物上时显示消息 - Display message when hovering over something with mouse cursor in Python Python tkinter - 放置在坐标处时按钮不出现 - Python tkinter - button not appearing when placed at a coordinate 当光标悬停在按钮上时,如何为按钮设置动画? - How to animate a button on kivy when the cursor is over it? Selenium/Python - 如何单击仅在光标悬停在其上时才出现的按钮? - Selenium/Python - How to click on a button that appears only when cursor is hovering over it? 将光标悬停在画布上时如何显示画布坐标? - How to display canvas coordinates when hovering cursor over canvas? 寻找放置在黑白对象上时反转文本颜色的方法 - looking for method to invert color of text when placed over black & white objects 当用户将鼠标 cursor 放在链接上时,如何在文本小部件中显示手 Cursor - How to show a Hand Cursor inside Text Widget when the User puts the mouse cursor over the Link (Python)Platformer-角色跳过/放置不正确 - (Python) Platformer - character skips over/incorrectly placed Python Tkinter:有没有办法让文本和按钮放置在 x 和 y 轴上? - Python Tkinter: Is there a way to have text and buttons with the button placed on the x and y axes? 当 cursor 悬停在 Python (tkinter) 中的 canvas 时显示消息 - Display message when the cursor is hovering a canvas in Python (tkinter)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM