Ds Arjun
Ds Arjun

Reputation: 459

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. Is there any other way to proceed for displaying when cursor placed on button or any modifications??

Upvotes: 2

Views: 5291

Answers (2)

Maxwell Grady
Maxwell Grady

Reputation: 316

As far as I know, Tk doesn't have a built in construct for button ToolTips

In Qt (PyQt), which is another GUI framework, this a a built in feature - for example:

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

Essentially you create your own class ToolTip() and a function in your module to add a new ToolTip, addToolTip()

Here are two references for doing this: 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.

Upvotes: 3

Bryan Oakley
Bryan Oakley

Reputation: 385810

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)

Upvotes: 3

Related Questions