简体   繁体   中英

How to position TopLevel Window in Tkinter?

I want to position my Top Level Window just to the right of my Root Window.
Something like this → ❏❏

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}")

You could use .winfo_x() and .winfo_width() to get the offsetx of the toplevel window. Like:

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

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