Search code examples
pythontkinterwindowscreentoplevel

Is there any possibility to spawn new window in tkinter on the same screen as his parent?


I'm using python tkinter on Windows and I want to spawn a new Toplevel window on the same screen as his parent. For example when user has 2 monitors I want to spawn every new window on the same monitor as main app window.

Is there any possibility to achieve this result?


Solution

  • You can check the parent window's pixel coordinates by winfo_x and winfo_y, and then spawn the Toplevel at the same location.

    import tkinter as tk
    
    root = tk.Tk()
    
    def get_geometry():
        top = tk.Toplevel()
        top.geometry(f"+{root.winfo_x()}+{root.winfo_y()}")
        top.title("This is new toplevel")
    
    tk.Button(root,text="Spawn new window",command=get_geometry).pack()
    
    root.mainloop()