Home > other >  Tkinter destroying a window (root) gives error
Tkinter destroying a window (root) gives error

Time:02-03

Whenever I call root.destroy(), I get an error irrelevant to the code. I have no clue why it's erroring when I try to close the window.

New, updated code including all functions:

import pyautogui
import database
import customtkinter, tkinter
import settings
import os
import threading

def loginUI():
    loginRoot = customtkinter.CTk()
    loginRoot.geometry("650x175")
    loginRoot.title("Bazaar Bot - Login")
    loginRoot.resizable(False, False)

    def Login():
        username = userentry.get()
        password = passentry.get()
        res = database.login(username, password)
        if res == "Success!":
            ExitWindow(loginRoot)
            tkinter.messagebox.showinfo(res, "Successfully logged in.")
            mainUI()
        else:
            tkinter.messagebox.showerror("Error", res)
    def gotoRegister():
        loginRoot.destroy() 
        registerUI() #error does not happen when I remove this line.

    userlabel = customtkinter.CTkLabel(master=loginRoot, text="Username:", font=font1)
    userlabel.place(x=15, y=10)
    passlabel = customtkinter.CTkLabel(master=loginRoot, text="Password:", font=font1)
    passlabel.place(x=15, y=50)

    userentry = customtkinter.CTkEntry(master=loginRoot, font=font1, width=535)
    userentry.place(x=100, y=10)
    passentry = customtkinter.CTkEntry(master=loginRoot, font=font1, width=535, show="•")
    passentry.place(x=100, y=50)

    lostpassword = customtkinter.CTkButton(master=loginRoot, text="Lost password?", font=font2, width=620, height=20, fg_color="transparent")
    lostpassword.pack()
    lostpassword.place(x=15, y=85)

    register = customtkinter.CTkButton(master=loginRoot, text="Want to register with serial key?", font=font2, width=620, height=20, fg_color="transparent", command=gotoRegister)
    register.pack()
    register.place(x=15, y=110)

    login = customtkinter.CTkButton(master=loginRoot, text="Login", font=font1, width=620, command=Login)
    login.pack()
    login.place(x=15, y=140)

    loginRoot.mainloop()

invalid = [' ', ',', ' ', '-', '=', '\'', '/', '(', ')', '[', ']', '{', '}']

def registerUI():
    def Register():
        if len(userentry.get()) <= 4:
            tkinter.messagebox.showerror("Error", "Username must be at least 5 characters.")
            return
        if len(passentry1.get()) <= 4:
            tkinter.messagebox.showerror("Error", "Password must be at least 5 characters.")
            return
        if passentry1.get() != passentry2.get():
            tkinter.messagebox.showerror("Error", "Passwords are not the same.")
            return
        if len(emailentry1.get()) <= 3:
            tkinter.messagebox.showerror("Error", "Please input a valid email address.")
            return
        if emailentry1.get() != emailentry2.get():
            tkinter.messagebox.showerror("Error", "Emails are not the same.")
            return
        for i in invalid:
            if i in userentry.get():
                tkinter.messagebox.showerror("Error", "Username cannot contain '"   i   "'")
                return
        for i in invalid:
            if i in emailentry1.get():
                tkinter.messagebox.showerror("Error", "Email cannot contain '"   i   "'")
                return

        key = licenseentry.get()
        username = userentry.get()
        password = passentry1.get()
        email = emailentry1.get()

        if database.checkuserexists(username):
            tkinter.messagebox.showerror("Error", "Username already in use.")
        if database.checkemailexists(email):
            tkinter.messagebox.showerror("Error", "Email already in use.")
        status = database.checklicensekey(key, username, password, email)
        if status == "Success! Please log in.":
            tkinter.messagebox.showinfo("Success!", status)
            ExitWindow(registerRoot)
            loginUI()
        else:
            tkinter.messagebox.showerror("Error", status)
        #print() #key isn't used already, key is linked to owner

    registerRoot = customtkinter.CTk()
    registerRoot.geometry("650x375")
    registerRoot.title("Bazaar Bot - Registration")
    registerRoot.resizable(False, False)

    userlabel = customtkinter.CTkLabel(master=registerRoot, text="Username:", font=font1)
    userlabel.place(x=15, y=10)
    userentry = customtkinter.CTkEntry(master=registerRoot, font=font1, width=480)
    userentry.place(x=160, y=10)

    passinfo = customtkinter.CTkLabel(master=registerRoot, text="(Make sure you remember your password! You will need it to log in again.)", font=font3, text_color='#808080')
    passinfo.place(x=160, y=60)
    passlabel1 = customtkinter.CTkLabel(master=registerRoot, text="Password:", font=font1)
    passlabel1.place(x=15, y=80)
    passentry1 = customtkinter.CTkEntry(master=registerRoot, font=font1, width=480, show="•")
    passentry1.place(x=160, y=80)
    passlabel2 = customtkinter.CTkLabel(master=registerRoot, text="Confirm Password:", font=font1)
    passlabel2.place(x=15, y=120)
    passentry2 = customtkinter.CTkEntry(master=registerRoot, font=font1, width=480, show="•")
    passentry2.place(x=160, y=120)

    emailinfo = customtkinter.CTkLabel(master=registerRoot, text="(Make sure you put in a valid email address! You will need to verify it later.)", font=font3, text_color='#808080')
    emailinfo.place(x=160, y=170)
    emaillabel1 = customtkinter.CTkLabel(master=registerRoot, text="Email:", font=font1)
    emaillabel1.place(x=15, y=190)
    emailentry1 = customtkinter.CTkEntry(master=registerRoot, font=font1, width=480)
    emailentry1.place(x=160, y=190)
    emaillabel2 = customtkinter.CTkLabel(master=registerRoot, text="Confirm Email:", font=font1)
    emaillabel2.place(x=15, y=230)
    emailentry2 = customtkinter.CTkEntry(master=registerRoot, font=font1, width=480)
    emailentry2.place(x=160, y=230)

    licenseinfo = customtkinter.CTkLabel(master=registerRoot, text="(Your license key was sent to you via email when you purchased bazaarbot.)", font=font3, text_color='#808080')
    licenseinfo.place(x=160, y=270)
    licenselabel1 = customtkinter.CTkLabel(master=registerRoot, text="License Key:", font=font1)
    licenselabel1.place(x=15, y=290)
    licenseentry = customtkinter.CTkEntry(master=registerRoot, font=font1, width=480, show="•")
    licenseentry.place(x=160, y=290)

    register = customtkinter.CTkButton(master=registerRoot, text="Register", font=font1, width=620, command=Register)
    register.pack()
    register.place(x=15, y=340)

    registerRoot.mainloop()

loginUI()

Here is pretty much all of the code, the error happens when I call registerUI(). Without this line, the loginUI window closes fine by itself.

error: invalid command name "1693004801088check_dpi_scaling" while executing "1693004801088check_dpi_scaling" ("after" script) invalid command name "1693027104576update" while executing "1693027104576update" ("after" script) invalid command name "1693028003328_click_animation" while executing "1693028003328_click_animation" ("after" script)

Process finished with exit code 0

gif

CodePudding user response:

The issue is that you are destroying root before calling the mainloop()

If you do it like this for example it works because the root is only destroyed after the button is clicked and that is only possible when the window is already drawn

import customtkinter


def ExitWindow():
    print("exit")
    root.destroy()  # <---- error cause


root = customtkinter.CTk()  # custom tkinter, similar to tkinter
root.geometry("650x375")
root.title("Bazaar Bot - Registration")
root.resizable(False, False)

# some extra code here, irrelevant.

button = customtkinter.CTkButton(root, command=ExitWindow)
button.pack()

root.mainloop()

CodePudding user response:

call mainloop without root

from tkinter import mainloop
# not to use registerRoot.mainloop()
mainloop()
  • Related