2

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??

Ds Arjun
  • 409
  • 2
  • 7
  • 16
  • 2
    See [_What is the simplest way to make tooltips in Tkinter_](http://stackoverflow.com/questions/3221956/what-is-the-simplest-way-to-make-tooltips-in-tkinter). – martineau Jun 01 '15 at 17:31
  • @martineau: I'm not sure the OP is asking about a tooltip. They have a static label widget they want to display messages in (I think...) – Bryan Oakley Jun 01 '15 at 19:44
  • @Bryan: They may using a static label widget because that's the only way they've thought of to do it — but of course I could be wrong (and is why I didn't flag the question as a duplicate). – martineau Jun 01 '15 at 20:09
  • Possible duplicate of [What is the simplest way to make tooltips in Tkinter?](https://stackoverflow.com/questions/3221956/what-is-the-simplest-way-to-make-tooltips-in-tkinter) – Stevoisiak Feb 21 '18 at 19:16

2 Answers2

3

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.

Community
  • 1
  • 1
Maxwell Grady
  • 306
  • 4
  • 12
  • I'd be happy to change something if I have misspoken or said something that was simply incorrect - the first reference I posted solves the issue in a very similar manner by providing binding and to some defined function to show text. Not sure what the issue was but apologies if I said something wrong – Maxwell Grady Jun 01 '15 at 22:20
  • my point was just questioning why I was down-voted without any comments - I'm totally ok with getting down-votes if there's some discussion of why my answer was not correct or not relevant. I just dislike seeing random down-votes with no discussion.Its harder for people to learn that way. – Maxwell Grady Jun 02 '15 at 21:52
  • Maxwell Grady, i didnt down vote for that. it was really helpful for me. Thank you very much. – Ds Arjun Jun 03 '15 at 18:22
3

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)
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685