请考虑以下代码:
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()
鉴于http://effbot.org/tkinterbook/widget.htm下“winfo_containing”下的信息,我希望这会打印entry
的身份。例如。就像是 ””。相反,我总是得到None
。为什么是这样?该条目是screen
的孩子,坐标(5,5)属于我place
号召唤中指定的位置。
或者,是否有不同的方法可以将(最顶层)小部件放在某个位置?
答案 0 :(得分:2)
jasonharper在他的评论中说的是正确的,winfo_containing
采用相对于实际计算机屏幕的坐标,而不是tkinter GUI,你需要先调用update_idletasks
才能得到正确答案:
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()