Display message when hovering over something with mouse cursor in Python

前端 未结 7 795
走了就别回头了
走了就别回头了 2020-12-01 10:57

I have a GUI made with TKinter in Python. I would like to be able to display a message when my mouse cursor goes, for example, on top of a label or button. The purpose of th

7条回答
  •  囚心锁ツ
    2020-12-01 11:16

    I have a very hacky solution but it has some advantages over the current answers so I figured I would share it.

    lab=Label(root,text="hover me")
    lab.bind("",popup)
    
    def do_popup(event):
        # display the popup menu
        root.after(1000, self.check)
        popup = Menu(root, tearoff=0)
        popup.add_command(label="Next")
        popup.tk_popup(event.x_root, event.y_root, 0)
    
    def check(event=None):
        x, y = root.winfo_pointerxy()
        widget = root.winfo_containing(x, y)
        if widget is None:
            root.after(100, root.check)
        else:
            leave()
    
    def leave():
        popup.delete(0, END)
    

    The only real issue with this is it leaves behind a small box that moves focus away from the main window If anyone knows how to solve these issues let me know

提交回复
热议问题