0

Im trying to close my login wondow and open my register window once I click the sign up button but it doesn't destroy the login window

Here is my login code

from tkinter import *
from tkinter import ttk
from tkinter import messagebox
import PIL
from PIL import ImageTk
from PIL import Image
import register

import pymysql


class Login:
    def __init__(self,root):
        self.root = root
        self.root.title("Scheduling Management System")
        self.root.geometry("1350x768+0+0")
        self.root.resizable(False, False)

        self.txt_user = StringVar()
        self.txt_pass = StringVar()
        self.bg = ImageTk.PhotoImage(file="Images/bgimage.jpg")
        bg = Label(self.root, image=self.bg).place(x=0, y=0, relwidth=1, relheight=1)
        framelogin = Frame(self.root, bg="white")
        framelogin.place(x=450, y=100, height=500, width=700)

        title = Label(framelogin, text="Login Here", font=("Arial", 30, "bold"), fg="orange", bg="white").place(x=90,
                                                                                                                y=30)
        nexttitle = Label(framelogin, text="Scheduling Staff System", font=("Times New Roman", 18, "bold"), fg="orange",
                          bg="white").place(x=90, y=100)

        userlabel = Label(framelogin, text="Username", font=("Arial", 15, "bold"), fg="gray", bg="white").place(x=90,
                                                                                                                y=140)
        self.txt_user = Entry(framelogin, textvariable=self.txt_user, font=("times new roman", 15), bg="lightgray")
        self.txt_user.place(x=90, y=170, width=350, height=35)

        passlabel = Label(framelogin, text="Password", font=("Arial", 15, "bold"), fg="gray", bg="white").place(x=90,
                                                                                                                y=210)
        self.txt_pass = Entry(framelogin, textvariable=self.txt_pass, font=("times new roman", 15), show="*",
                              bg="lightgray")
        self.txt_pass.place(x=90, y=240, width=350, height=35)

        forget = Button(framelogin, text="Forgot Password", bg="white", fg="orange", font=("trebuchet ms", 12)).place(
            x=90, y=305)
        reglabel = Label(framelogin, text="Don't Have an Account?", font=("trebuchet ms", 12, "bold"), fg="orange",
                         bg="white").place(x=320, y=310)
        registerbutton = Button(framelogin, text="Sign Up", command=self.register, bg="white", fg="orange",
                                font=("trebuchet ms", 12)).place(x=510, y=305)

        loginbutton = Button(framelogin, text="Login", command=self.login, fg="white", bg="orange",
                             font=("sans serif ms", 20)).place(x=90, y=350, width="100", height="40")

    def login(self):
        if self.txt_user.get() == "" or self.txt_pass.get() == "":
            messagebox.showerror("Error", "Please fill up all fields!")

    def register(self):
        register.RegisterForm()




if __name__ == "__main__":
    root = Tk()
    obj = Login(root)
    root.mainloop()

Here is my register code

from tkinter import *
from tkinter import ttk, messagebox

import PIL
import pymysql
from PIL import ImageTk
from PIL import Image
import login

class Register:
    def __init__(self, root):
        self.root = root
        ...

        btn = Button(frame1,image=self.btn, bd=0, command = self.registerdata,cursor = "hand2").place(x=50, y = 420)


    def registerdata(self):
        if self.text_fname.get()=="" or self.text_lname.get()=="" or self.text_contact.get()=="" or self.text_email.get()=="" or self.cmbquestion.get()=="Select" or self.text_pwd.get()=="" or self.text_cfmpwd.get()=="":
            messagebox.showerror("Error","All fields are required!",parent=self.root)
        elif self.text_pwd.get()!=self.text_cfmpwd.get():
            messagebox.showerror("Error","Passwords must be the same!",parent=self.root)
        else:
            try:
                con=pymysql.connect(host="localhost",user="root",password="",database="employee")
                cur=con.cursor()
                cur.execute("select * from employeelist where email=%s", self.text_email.get())
                row=cur.fetchone()
                print(row)
                if row!=None:
                    messagebox.showerror("Error","User Already Exists. Please Register With a New Email",parent=self.root)
                else:
                    cur.execute("insert into employeelist (fname,lname,contact,email,question,answer,password) values(%s,%s,%s,%s,%s,%s,%s)",
                                (self.text_fname.get(),self.text_lname.get(),self.text_contact.get(),self.text_email.get(),self.cmbquestion.get(),self.text_answer.get(),self.text_pwd.get()))
                    con.commit() #do changes to database
                    con.close()
                    messagebox.showinfo("Success","Registration Successful",parent=self.root)
            except Exception as ex:
                messagebox.showerror("Error",f"Error due to: {str(ex)}",parent=self.root)
        l = login
        l.Login(root)

def RegisterForm():
    win = Toplevel()
    obj = Register(win)

if __name__ == "__main__":
    root = Tk()
    obj = Register(root)
    root.mainloop()

Any ideas on how to do so as currently I'm only able to see both windows instead of destroying login window and opening register window?

  • add `self.root.destroy()` to the `register` method – Henry Jan 29 '21 at 15:45
  • A common way is to not destroy old windows, but to just substitute the contents. Have a look at [Switch between two frames in tkinter](https://stackoverflow.com/questions/7546050/switch-between-two-frames-in-tkinter). – figbeam Jan 29 '21 at 15:47
  • @Henry when i do that it works but another new small window appears with no content and when I close that small window my whole window closes as well I have added the line to the register method `def register(self): self.root.destroy() register.RegisterForm()` – Jagdeesh Sandhu Jan 30 '21 at 03:19
  • @JagdeeshSandhu I've had a look at your code again, I assume it's the same code as [this question](https://stackoverflow.com/questions/65956985/close-current-window-and-open-new-window), but there is no `register.RegisterForm()` in register.py so I can't debug it any further. Please post the code for `RegisterForm.` – Henry Jan 30 '21 at 11:03
  • @Henry I have made the changes to the code and have added the code for `RegisterForm` – Jagdeesh Sandhu Jan 30 '21 at 12:32

1 Answers1

1

Firstly add self.root.destroy() to the register method in the login file. This will destroy the login Tk instance. To avoid the extra window when the register window opens, change win = Toplevel() in RegisterForm to win = Tk(). Toplevel needs a Tk window so it made an extra window, so we need to create a Tk instance instead.

Henry
  • 3,472
  • 2
  • 12
  • 36