0

I am having a problem in positioning the login window in center of my screen.

This is my code:

from customtkinter import *
from PIL import Image
from tkinter import messagebox
from views.registration import RegistrationWindow
from views.homepage import HomepageWindow
from views.admin import AdminWindow
from models.database import authenticate_user

class LoginApp(CTk):
    def __init__(self):
        super().__init__()
        self.title("Login")
        self.setup_window()
        self.create_widgets()

    def setup_window(self):
        height = 430
        width = 530

        x = (self.winfo_screenwidth() // 2) - (width // 2)
        y = (self.winfo_screenheight() // 2) - (height // 2)

        self.geometry(f"{width}x{height}+{x}+{y}")

    def create_widgets(self):
        image_path = "images/prdp_logo.png"
        image = CTkImage(dark_image=Image.open(image_path), size=(150, 150))
        image_label = CTkLabel(master=self, image=image, text="")
        image_label.place(relx=0.5, rely=0.3, anchor="center")
        
        self.username_entry = CTkEntry(master=self, placeholder_text="Username", font=("Arial", 12), text_color="white", width=300)
        self.username_entry.place(relx=0.5, rely=0.5, anchor="center")

        self.password_entry = CTkEntry(master=self, placeholder_text="Password", font=("Arial", 12), text_color="white", width=300, show="*")
        self.password_entry.place(relx=0.5, rely=0.6, anchor="center")

        CTkButton(master=self, text="LOGIN", font=("Arial", 12), text_color="white", width=200, command=self.login).place(relx=0.5, rely=0.7, anchor="center")
        CTkButton(master=self, text="REGISTER", font=("Arial", 12), text_color="white", width=200, command=self.open_registration_window).place(relx=0.5, rely=0.8, anchor="center")

    def open_registration_window(self):
        RegistrationWindow(self)

    def login(self):
        username = self.username_entry.get().strip()
        password = self.password_entry.get().strip()

        if not username or not password:
            messagebox.showerror("Error", "Please enter your username and password!")
            return

        result = authenticate_user(username, password)
        if result:
            fullname, role = result
            self.withdraw()
            if role == "ADMIN":
                admin_panel = AdminWindow(fullname, self)
                admin_panel.mainloop()
            else:
                homepage = HomepageWindow(fullname, self)
                homepage.mainloop()
        else:
            messagebox.showerror("Error", "Invalid username or password!")

I came up with the idea on changing the scale in display settings. My current scale is 125% which is the recommended. Then I tried changing it to 100% and it worked, the window is now positioned in the center of my screen. But if I change it back to 125% it goes back to the same problem not being centered again. I tried testing other applications in 125% scale and they are centered on my screen upon opening. I don't know why my application doesn't do the same.

1
  • Try calling self.update_idletasks() before calling setup_window and after calling create_widgets. Creating widgets affects TK geometry. Commented Jan 30 at 13:43

1 Answer 1

0

Tkinter processes positions weird when you're not on 100% scaling.

Here's how to make tkinter "aware" of the current DPI:

from ctypes import windll
windll.shcore.SetProcessDpiAwareness(2)

Make sure this code is ran before creating the window. Bonus: it also makes the window not blurry anymore!

Source

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.