简体   繁体   中英

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

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()

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM