如何在Tkinter中放置顶层窗口?

问题描述 投票:1回答:1

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

这是我的根窗口的代码

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()

这是我的TopLevel窗口的代码

newWindow = Toplevel(pady=10)
newWindow.geometry(f"+{root_width+root_xoffset}+{root_yoffset}")
python python-3.x tkinter
1个回答
1
投票

您可以使用.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()
© www.soinside.com 2019 - 2024. All rights reserved.