0

Iam new to Tkinter and am trying do an interface a small randomising application. i have several windows and i need to open one at a time, however the problem is when a user enters wrong crentials and they have to go back to the login page which was destroyed previous. When i run my code so far, i get the error below:

Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\SANDRA\AppData\Local\Programs\Python\Python37\lib\tkinter__init__.py", line 1705, in call return self.func(*args) File "C:\Users\SANDRA\Desktop\python work\GUI\stem page 1.py", line 31, in validation open_login() File "C:\Users\SANDRA\Desktop\python work\GUI\stem page 1.py", line 35, in open_login root.destroy() File "C:\Users\SANDRA\AppData\Local\Programs\Python\Python37\lib\tkinter__init__.py", line 2062, in destroy self.tk.call('destroy', self._w) _tkinter.TclError: can't invoke "destroy" command: application has been destroyed

below is my code so far:

from tkinter import *

    #creating the admin's logged in page
    def admin_logged_in():
        #global adn_logged_in

        adn_logged_in = Tk()
        adn_logged_in.title("Administrative page")
        adn_logged_in.config(bg = "#ffe6ff")
        adn_logged_in.geometry("700x400")
        adn_logged_in.resizable(width=FALSE, height=FALSE)
        adn_lbl = Label(adn_logged_in, text ="please select what you wish to do.", font = "Arial 16",fg = "black", bg = "#ffe6ff", pady = 50)
        adn_lbl.pack()
        list = Listbox(adn_logged_in,selectmode = SINGLE, height = 2,bg = "#ffe6ff", width = 30, font = "Arial 16" ,fg = "blue")
        list.insert(1, "Add students")
        list.insert(2, "Delete students")
        list.pack()

        adn_logged_in.mainloop()

    #creating a function to validate the username and password
    def validation():
        username_text = username.get()
        password_text = password.get()
        if username_text == "admin" and password_text == "123":
            admin.destroy()
            admin_logged_in()

        else: 
            open_login()

    #creating a function to open the administrator window
    def open_login():
        root.destroy()
        global password
        global username
        global admin
        admin = Tk()#creates a new window
        admin.title("Administrative login page")
        admin.config(bg = "#ffe6ff")
        admin.geometry("700x400")
        admin.resizable(width=FALSE, height=FALSE)

        label1 = Label(admin,text = " please login", font = "Arial 40",fg = "blue", bg = "#ffe6ff", pady = 40)
        label1.pack()
        open_frame = Frame(admin, bg = "#ffe6ff")
        open_frame.pack()

    #crating label and entry for user name .
        name_lbl = Label(open_frame, text = "Username: ",font = "Arial 16", fg = "black",bg = "#ffe6ff",)
        name_lbl.grid(row = 0, column = 0, sticky = "e")
        username = StringVar()
        name_entry = Entry(open_frame, textvariable = username, width = 50)
        name_entry.grid(row = 0, column = 1, sticky = "w", pady =8)

    #creating label and entry for password
        pw_lbl = Label(open_frame, text = "Password: ",font = "Arial 16", fg = "black", bg = "#ffe6ff",)
        pw_lbl.grid(row = 1, column = 0, sticky = "e")
        password = StringVar()
        pw_entry = Entry(open_frame, textvariable = password,width = 50, show = "*")
        pw_entry.grid(row = 1, column = 1, sticky = "w",pady = 8)
    #creating a submit button
        sub_btn = Button(open_frame, text = "Submit",font = "Arial 16", fg = "black", bg = "#ffe6ff", width = 20,command= validation)
        sub_btn.grid(row = 3, column = 1, sticky = "s")

        admin.mainloop()

    #creating the main window
    root = Tk()
    root.title("Student Randomiser v 1.0")
    root.config(bg = "#ffe6ff")
    root.geometry("700x400")
    root.resizable(width=FALSE, height=FALSE)

    #creating the frame1 for the first window
    frame1 = Frame(relief = FLAT, borderwidth = 3, bg ="#ffe6ff")
    frame1.pack()
    #creating the heading
    main_lbl = Label(frame1, text = "STUDENT RANDOMISER",bg = "#ffe6ff",fg = "blue", font = "Helvetica 40 bold")
    main_lbl.pack(side = TOP, pady= 40)

    #creating the welcome note
    welcome_lbl = Label(frame1,font = "Arial 18", text = "Welcome to our student randomising sytem ",bg = "#ffe6ff",fg = "black")
    welcome_lbl.pack()

    #creatinf empty frame for spacing
    empty_frame = Frame(height = 40)
    empty_frame.pack()
    #creating a frame for the buttons
    frame2 = Frame(relief = FLAT,borderwidth = 3, bg ="#ffe6ff")
    frame2.pack()
    #creating a adminstrator button
    adm_button = Button(frame2, font = "Arial 16",text = "Adminstrator", fg = "black", width = 20, command =open_login)
    adm_button.pack(side = LEFT, padx = 15, pady = 5)

    #creating user button
    user_button = Button(frame2, font = "Arial 16", text = "User", fg = "black", width = 20)
    user_button.pack(side = RIGHT, padx = 15, pady = 5)


    root.mainloop()
N.Sandra
  • 1
  • 1
  • There is no way to get back the window that you have destroyed. The window has to be re-invoked. Rather than switch between windows, [switching between frames](https://stackoverflow.com/questions/7546050/switch-between-two-frames-in-tkinter) is fairly straight-forward. – FrainBr33z3 Feb 27 '20 at 11:43
  • use `.withdraw()` instead of destroy. – CC7052 Feb 27 '20 at 13:33

0 Answers0