pythontkintertoplevel

New window is offset from old window


I'm having trouble when closing and opening new windows in python. The new window is always offset from the old window. As I'm using a small display for the project I need the position of the new window to be the same as the old window.

Here's what the first window looks like.

enter image description here

When the "login" button is clicked, the first window is deleted and a new window generated. Notice the offset.

enter image description here

It's not a huge offset, but enough to cause problems in the small display.

Here's the code I'm using:

from tkinter import Tk, Toplevel, Button


def login_window(parent):

    def login():
        tp.destroy()
        parent.deiconify()

    tp = Toplevel()
    tp.title('Login Window')
    tp.geometry('480x320')
    tp.protocol('WM_DELETE_WINDOW', parent.quit)

    Button(tp, text='Login', command=login).pack()


def main_window():
    root = Tk()
    root.title('Main Window')
    root.geometry('480x320')
    root.withdraw()

    login_window(root)

    root.mainloop()


if __name__ == '__main__':
    main_window()

I haven't been able to google a fix for this. What am I missing?


Solution

  • You must change in the root.geometry from root.geometry('widthxheight') to root.geometry("widthxheight+position_x+position_y")for example root.geometry("480x320+0+0").