Python Forum

Full Version: Tkinter optionmenu child menu position showing 0,0
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everyone,

I have an optionmenu widget and I am attempting to place a toplevel window inline with each option as the user hover's over each one, effectively creating a tooltip for each option. I have code that can effectively do this. The problem is that I am having issues setting the position. I can't seem to grab the current menu items position.

I don't know if it is kosher to post links to other websites but my problem was asked on stackoverflow with no answers basically. I'll repost the question here:

----

I am trying to get the position of the menu that is the child of the option menu widget in tkinter. I want to do this so that I can know when the menu has been shifted due to location on the screen (see picture 1)

[Image: qZPbz.png]

However, when I try and use the menu widget winfo_rootx or winfo_x they both just show position 0 regardless of where the menu actually is. See example code:

from tkinter import Tk, Frame, BOTH, Menu, Label, SUNKEN, X, BOTTOM
import tkinter as tk
class Application(Frame):
    def __init__(self, parent):
        Frame.__init__(self, parent, background = "white")
        self.parent = parent
        self.parent.geometry("400x100")
        vals = ["1","2","3","4","5","6","7"]
        var = tk.StringVar()
        var.set("1")
        option = tk.OptionMenu(root,var,*vals)
        option.pack()
        self.t = option.children["menu"]
        tk.Menu
        #Do I need to unbind
        #t.bind("<<MenuSelect>>", self.test_func)
        self.t.bind("<<MenuSelect>>", self.test_func)

    def test_func(self,event = None):
        print(self.t.winfo_children())
        print("x,y position",event.widget.winfo_x(),event.widget.winfo_y())
        print("x,y root position",event.widget.winfo_rootx(),event.widget.winfo_rooty())
        #if self.parent.call(event.widget,"index","active") == 0:
            #print(self.t)
root = tk.Tk()
Application(root)
root.mainloop()
I have potentially other options to try and fix this but it seems that there should be a way to get the menu widgets position correctly. My other way is rather hacky and uses the position and lengths of the widgets to try and calculate the adjusted position.
Quote:However, when I try and use the menu widget winfo_rootx or winfo_x they both just show position 0 regardless of where the menu actually is. See example code:
What exactly were you expecting winfo... winfo.children displays a list of the children. You posibly want winfo.x and winfo.y
(Jun-15-2018, 02:00 AM)woooee Wrote: [ -> ]
Quote:However, when I try and use the menu widget winfo_rootx or winfo_x they both just show position 0 regardless of where the menu actually is. See example code:
What exactly were you expecting winfo... winfo.children displays a list of the children. You posibly want winfo.x and winfo.y

I am not sure I understand you because in the method test_func I print 3 statements, one of which is winfo_children, the other two of which should be the position of the menu widget associated with the optionmenu widget.