Python Forum
[Tkinter] Tkinter optionmenu child menu position showing 0,0
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Tkinter optionmenu child menu position showing 0,0
#1
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.
Reply
#2
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
Reply
#3
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problems trying to position images with Tkinter emont 3 667 Dec-12-2023, 07:20 AM
Last Post: menator01
  [Tkinter] tkinter menubar not showing on mac ventura taras 1 2,043 Dec-17-2022, 02:44 PM
Last Post: Yoriz
  [PyQt] QWidgetAction of QFrame not showing in menu malonn 4 1,941 Sep-22-2022, 10:49 PM
Last Post: malonn
  tkinter showing image in button rwahdan 3 5,517 Jun-16-2021, 06:08 AM
Last Post: Yoriz
  Tkinter reduce OptionMenu height euras 2 4,567 May-25-2021, 09:14 PM
Last Post: euras
  tkinter text widget word wrap position chrisdb 6 7,442 Mar-18-2021, 03:55 PM
Last Post: chrisdb
  [Tkinter] tkinter.Menu – How to make text-variable? Sir 3 5,538 Mar-10-2021, 04:21 PM
Last Post: Sir
  tkinter python button position problem Nick_tkinter 3 3,482 Jan-31-2021, 05:15 AM
Last Post: deanhystad
  [Tkinter] Tkinter Textbox only showing one character Xylianth 1 2,141 Jan-29-2021, 02:59 AM
Last Post: Xylianth
  Tkinter menu font size -method to change tonycat 2 7,700 Oct-11-2020, 02:43 AM
Last Post: tonycat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020