TomSchimansky / CustomTkinter

A modern and customizable python UI-library based on Tkinter
MIT License
11.52k stars 1.08k forks source link

error when destroying a window #2058

Open PunkDevloPer opened 1 year ago

PunkDevloPer commented 1 year ago

I'm creating a new window from another one and then destroying the old one, but CTK gives me some strange errors even though the window is closed image the error appears to occur with self.destroy() so I'm not sure if I'm using it the right way

this is my code

""" importar librerias """ import os import customtkinter as ctk import tkinter as tk from PIL import Image import database import password_management as pswd

class Root(ctk.CTk): def init(self): super().init() self.geometry("300x350") self.resizable(False, False) self.login()

def login(self):
    login_image = ctk.CTkImage(light_image=Image.open("images/logn.png"),
                               size=(90, 90))

    ctk.CTkLabel(self, image=login_image, text="").pack()

    user_label = ctk.CTkLabel(
        self, text="Usuario", font=("Arial", 16), height=30)
    user_label.pack()
    self.user_entry = ctk.CTkEntry(
        self, height=40, width=250, corner_radius=10)
    self.user_entry.pack()

    user_pass = ctk.CTkLabel(
        self, text="Contraseña", font=("Arial", 16), height=30)
    user_pass.pack()
    self.pass_entry = ctk.CTkEntry(
        self, show="*", height=40, width=250, corner_radius=10)
    self.pass_entry.pack()

    user_separator = ctk.CTkLabel(self, text="")
    user_separator.pack()

    login_button = ctk.CTkButton(
        master=self, text="Entrar", command=self.verificar_login, corner_radius=10)
    login_button.pack()

    register_label = ctk.CTkLabel(
        self, text="¿No tienes una cuenta?", font=("Arial", 16))
    register_label.pack()

    register_button = ctk.CTkButton(
        self, text="Registrarse", command=database.open_register, corner_radius=10)
    register_button.pack()

def verificar_login(self):
    usuario = self.user_entry.get()
    contrasena = self.pass_entry.get()

    # Verificar las credenciales aquí (puedes modificar esto con tu lógica de autenticación)
    if usuario == "admin" and contrasena == "":
        self.destroy()
        pswd.windowmanager()
EvanArgiro commented 1 year ago

I could be wrong here, but when you're calling self.destroy(), the method is going to destroy the Root object and all of it's descendants, thus closing out of the window. It might be better to use CTkFrame for the login and then destroy the frame once verification has been authorized. (Anyone can feel free to correct me if I am wrong, no hard feelings!)

PunkDevloPer commented 1 year ago

Thank you very much, you are absolutely right, I am sorry if it was a bother, but I have already solved it, thank you very much.

Harriswells68 commented 1 year ago

How did you solved it?

PunkDevloPer commented 1 year ago

I used withdraw instead of destroy in the function verificar_login

def verificar_login(self):
    usuario = self.user_entry.get()
    contrasena = self.pass_entry.get()
    if database.validate_user_exists(usuario, contrasena):
        self.withdraw()   <- HERE
        pswd.windowmanager()