如何在Tkinter中定位顶级窗口?

2024-10-05 14:30:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我想将顶层窗口放在根窗口的右侧。
像这样的→ ❏❏

This is the code for my root window

root = Tk()
root.geometry("+100+100")
root.update() 
root_width = root.winfo_width()
root_height = root.winfo_height()
root_xoffset = root.winfo_x()
root_yoffset = root.winfo_y()

And this the code for my TopLevel window

newWindow = Toplevel(pady=10)
newWindow.geometry(f"+{root_width+root_xoffset}+{root_yoffset}")

Tags: theformycoderootwindowthiswidth
1条回答
网友
1楼 · 发布于 2024-10-05 14:30:50

您可以使用.winfo_x().winfo_width()获取顶级窗口的偏移量。 比如:

import tkinter as tk

root = tk.Tk()

root.update() # to get the height and the offset of Tk window
toplevel = tk.Toplevel()
toplevel_offsetx, toplevel_offsety = root.winfo_x() + root.winfo_width(), root.winfo_y()
padx = 0 # the padding you need.
pady = 0
toplevel.geometry(f"+{toplevel_offsetx + padx}+{toplevel_offsety + pady}")
root.mainloop()

相关问题 更多 >