Skip to content Skip to sidebar Skip to footer

Ttk Menu Wont Unpost

I wanted to make a simple pop-up message that shows the name of the options represented as icons when the mouse enters them and hide when it leavs. This icons are images within but

Solution 1:

You can create a class that takes a widget and a message as parameters, and then apply to any widget requiring the info.

import tkinter as tk

root = tk.Tk()

classCreateToolTip:
    def__init__(self, widget, text='widget info'):
        self.waittime = 100#500     #miliseconds
        self.wraplength = 180#pixels
        self.widget = widget
        self.text = text
        self.widget.bind("<Enter>", self.enter)
        self.widget.bind("<Leave>", self.leave)
        self.widget.bind("<ButtonPress>", self.leave)
        self.id = None
        self.tw = Nonedefenter(self, event=None):
        self.schedule()

    defleave(self, event=None):
        self.unschedule()
        self.hidetip()

    defschedule(self):
        self.unschedule()
        self.id = self.widget.after(self.waittime, self.showtip)

    defunschedule(self):
        id = self.id
        self.id = Noneifid:
            self.widget.after_cancel(id)

    defshowtip(self, event=None):
        x = y = 0
        x, y, cx, cy = self.widget.bbox("insert")
        x += self.widget.winfo_rootx() + 25
        y += self.widget.winfo_rooty() + 40# creates a toplevel window
        self.tw = tk.Toplevel(self.widget)
        # Leaves only the label and removes the app window
        self.tw.wm_overrideredirect(True)
        self.tw.wm_geometry("+%d+%d" % (x, y))
        label = tk.Label(self.tw, text=self.text, justify='left',
                       background="#ffffff", relief='solid', borderwidth=1,
                       wraplength = self.wraplength)
        label.pack(ipadx=1)

    defhidetip(self):
        tw = self.tw
        self.tw= Noneif tw:
            tw.destroy()

a = tk.Button(root,text="Something")
a.pack()
CreateToolTip(a,"This is something button")
b = tk.Button(root,text="Another")
b.pack()
CreateToolTip(b,"This is another button")

root.mainloop()

Solution 2:

Aparently Windows does not recognize the unpost command (for more information: https://www.tcl.tk/man/tcl8.6/TkCmd/menu.htm#M45 & https://wiki.tcl-lang.org/page/How+do+you+unpost+a+menu)

The solution that I've come with is the the followin:

defDespliega(Texto):
    global MenuDesplegable
    MenuDesplegable = Label(master=None, bg="#F4F4F4", text=str(Texto),relief='ridge', bd=3, width=len(Texto)+2)
    x = Raiz.winfo_pointerx()
    y = Raiz.winfo_pointery()
    abs_coord_x = Raiz.winfo_pointerx() - Raiz.winfo_rootx()
    abs_coord_y = Raiz.winfo_pointery() - Raiz.winfo_rooty()
    MenuDesplegable.place(x = abs_coord_x, y = abs_coord_y)


defRepliega():
    global MenuDesplegable

    MenuDesplegable.destroy()

This tow methods places a Tkinter.Label at the mouse position at the time that the coursor gets into one of the buttons, and destoys it when it leves the button.

It works well, but it does rare things when the mouse is placed on top of the Label.

Post a Comment for "Ttk Menu Wont Unpost"