Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Python Tkinter: Always on top window isn't showing custom class tooltip text

Tags:

python

tkinter

I can't get tooltip to work with my always on top window. Obviously the problem is you can't create something on top of something that's always on top; so i was wondering if there was a workaround or solution. The Popup needs to be always on top of other windows, but i also need to have all the tooltips show up properly.

Here's a stripped down version of what I have so far:

from Tkinter import *

class GUI:
    def __init__(self, root):
        Popup = Toplevel(root)
        Popup.resizable(0,0)
        Popup.attributes("-toolwindow", 1)
        Popup.wm_attributes("-topmost", 1)
        PFrame = Frame(Popup)
        self.B = Button(PFrame, width=10,height=10)
        self.B.pack()
        self.createToolTip(self.B,"Click this button.")
        PFrame.pack()

    class ToolTip(object):
        def __init__(self, widget):
            self.widget = widget
            self.tipwindow = None
            self.id = None
            self.x = self.y = 0
        def showtip(self, text):
            self.text = text
            if self.tipwindow or not self.text: return
            x,y,cx,cy = self.widget.bbox("insert")
            x = x + self.widget.winfo_rootx() +15
            y = y + cy + self.widget.winfo_rooty() +65
            self.tipwindow = tw = Toplevel(self.widget)
            tw.wm_overrideredirect(1)
            tw.wm_geometry("+%d+%d"%(x,y))
            label = Label(tw, text=self.text, justify=LEFT)
            label.pack(ipadx=1)
        def hidetip(self):
            tw = self.tipwindow
            self.tipwindow = None
            if tw: tw.destroy()
    def createToolTip(self,widget,text):
        toolTip = self.ToolTip(widget)
        def enter(event): self.tt = root.after(1500,show,event)
        def show(event): toolTip.showtip(text)
        def leave(event):
            if self.tt: root.after_cancel(self.tt)
            toolTip.hidetip()        
        widget.bind('<Enter>', enter)
        widget.bind('<Leave>', leave)

if __name__ == '__main__':
    root = Tk()
    App = GUI(root)
    root.mainloop()
like image 246
Symon Avatar asked Feb 03 '12 20:02

Symon


People also ask

How do I display tooltips in tkinter?

Tooltips are useful in applications where we need to display some information while hovering on a button. In order to create and display a tooltip, we can use the Balloon property of tkinter.

How do you set the title text that appears at the very top of a tkinter window?

The title in tkinter refers to a name assigned to the application window. It is mostly found on the top of the application. For this, we can use the title() function. We create a widget object using the Tk() function and use the title() function to add a title to this window.

What is config () in tkinter?

config is used to access an object's attributes after its initialisation. For example, here, you define. l = Label(root, bg="ivory", fg="darkgreen") but then you want to set its text attribute, so you use config : l.

How do you make a tooltip in Python?

You can add a tooltip by calling . setToolTip("text") on a widget. This is often used to assist the user.


1 Answers

I fixed it by adding tw.wm_attributes("-topmost", 1) to the showtip function. Let me know if this solution is incorrect or if there is a better way.

like image 114
Symon Avatar answered Sep 21 '22 14:09

Symon