Home > Software engineering >  tkinter pack_forget vs Window default center position
tkinter pack_forget vs Window default center position

Time:12-05

I have an app with multiple windows. I use pack_forget to eliminate the login window and invoke the main window. However this main window loses the default centered position of tkinter. The window is created at position (0 , 0).

Is there any simple way to make this main window be created in the default centered position?

example code, 3 files ->

main.py

#!/usr/bin/env python3

from tkinter import *
from frm_login import Wlogin

class Mainframe(Tk):
    def __init__(self):
        Tk.__init__(self)
        self.frame = Wlogin(self)
        self.frame.pack()

    def change(self, frame):
        self.frame.pack_forget() # delete currrent frame
        self.frame = frame(self)
        self.frame.pack() # make new frame


if __name__== '__main__':
    app = Mainframe()
    app.mainloop()

frm_login.py

from tkinter import *
from frm_default import Wmain

class Func(Frame):
    def check(self, event=None):
        if self.pwd.get() == '1':
            self.master.change(Wmain)
        else:
            self.status.config(text='wrong password')


class Wlogin(Func):
    def __init__(self, master=None, **kwargs):
        Frame.__init__(self, master, **kwargs)

        master.title('Enter password')
        master.geometry('300x200')

        self.status = Label(self, fg='red')
        self.status.pack()

        self.lbl = Label(self, text='Enter password')
        self.lbl.pack()

        self.pwd = Entry(self, show='*')
        self.pwd.insert(-1, '1')
        self.pwd.pack()
        self.pwd.focus()
        self.pwd.bind('<Return>', self.check)
        self.pwd.bind('<KP_Enter>', self.check)

        self.btn = Button(self, text='Done', command=self.check)
        self.btn.pack()

        self.btn = Button(self, text='Cancel', command=self.quit)
        self.btn.pack()

frm_default.py

from tkinter import *

class Wmain(Frame):
    def __init__(self, master=None, **kwargs):
        Frame.__init__(self, master, **kwargs)
        master.title('Main application')
        master.geometry('600x400')

CodePudding user response:

There is nothing about your forget / repack code that makes this unique. You can use the same commands you would otherwise. So either define the position yourself:

master.geometry('600x400 300 400')

Or use tk PlaceWindow function:

master.eval('tk::PlaceWindow . center')

Or calculate the position from the window size and monitor size:

master.geometry("600x400")
master.update_idletasks()
x = (master.winfo_screenwidth() - master.winfo_reqwidth()) // 2
y = (master.winfo_screenheight() - master.winfo_reqheight()) // 2
master.geometry(f" {x} {y}")

FWIW, my experience tells me that setting the window size yourself instead of letting tkinter calculate it will lead to bugs down the road.

  • Related