简体   繁体   中英

winfo_container() always returning None?

Consider the following code:

import Tkinter as tk # Python 2.7
screen = tk.Tk()
entry = tk.Entry(screen)
entry.place(x=0, y=0, width=100, height=20)
print(screen.winfo_containing(5, 5))
screen.mainloop()

Given the information under "winfo_containing" at http://effbot.org/tkinterbook/widget.htm , I would expect this to print the identity of entry . Eg something like "". Instead, I always get None . Why is this? The entry is a child to screen , and the co-ordinate (5, 5) falls within the location specified in my call to place .

Alternatively, is there a different way of getting the (top-most) widget at a certain location?

What jasonharper says in his comment is right, winfo_containing takes coordinates relative to the actual computer screen, not the tkinter GUI, and you need to call update_idletasks before to get the right answer:

screen = tk.Tk()
entry = tk.Entry(screen)
entry.pack()
screen.update_idletasks()
x,y = screen.winfo_rootx(), screen.winfo_rooty()
print(screen.winfo_containing(x+5, y+5))
screen.mainloop()

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