-1

I'm sure the answer is very obvious, however I've been trying for so long now and I feel like I need some help. Basically, I have a main registration window in Tkinter to either press login or register. When I press one of these buttons, the login and register window appear which is what it is supposed to do, so that's fine. However, in the main registration window, the buttons duplicate everytime I click on one of the buttons. For example, when I press the login button, the login and register button duplicate in the main window everytime I click it.

from tkinter import *
import os
class main(object):
    def __init__(self):
        global screen
        self.screen = Tk()
        self.screen.geometry("500x300")
        self.screen.title("Login and Registration System")
        Label(text = "Welcome!", width = "300", height = "2", font = ("Calibri", 10)).pack()
        Label(text = "If you don't have a user, click on the register button to create an account.", width = "300", height = "2", font = ("Calibri", 10)).pack()
        Button(text = "Login", width = "30", height = "2", font = ("Calibri", 13), command = login).pack()
        Label(text = "").pack()
        Button(text = "Register", width = "30", height = "2", font = ("Calibri", 13), command = register).pack()
        Label(text = "").pack()
        #Button(text = "Guest", width = "30", height = "2", font = ("Calibri", 13), command = self.access).pack()
        #Label(text = "").pack()
    def access(self):
        self.screen3.destroy()
        global screen6
        self.screen6 = Toplevel(self.screen)
        self.screen6.title("Secret information")
        self.screen6.geometry("550x300")
        Label(self.screen6, text = "Welcome to the Mandelbrot Set Fractal visualisation tool").pack()
        Button(self.screen6, text = "exit", command = exit).pack()
    def run(self):
        self.screen.mainloop()
class destroy():
    '''
    def delete(self):
        self.screen2.destroy()
    def delete2(self):
        self.screen3.destroy()
    '''
    def delete3(self):
        self.screen4.destroy()
    def delete4(self):
        self.screen5.destroy()
    
    
class register(main):
    def __init__(self):
        super().__init__()
        global screen1
        self.screen1 = Toplevel()
        self.screen.withdraw()
        self.screen1.geometry("500x600")
        self.screen1.title("Register")

        global username
        global password
        global email
        global password2
        global usernameEntry
        global passwordEntry
        global emailEntry
        global confirmEntry

        self.username = StringVar()
        self.email = StringVar()
        self.c = StringVar()
        self.password = StringVar()
        self.password2 = StringVar()

        self.labelTitle = Label(self.screen1, text = "Create an account", font = ("Calibri", 20))
        self.labelTitle.place(x=50, y=3)

        self.labelTitle = Label(self.screen1, text = "Please, fill in the details below in order to register")
        self.labelTitle.place(x=90, y=50)
        self.labelTitle = Label(self.screen1, text = "* indicates a required field", fg = "red")
        self.labelTitle.place(x=140, y=73)

        self.labelUser = Label(self.screen1, text ="Username", width = "25")
        self.labelUser.place(x=30, y=130)
        self.labelRequired = Label(self.screen1, text="*", fg = "red")
        self.labelRequired.place(x=335, y=130)
        self.usernameEntry = Entry(self.screen1, textvariable = self.username)
        self.usernameEntry.place(x=170, y=130)

        self.labelEmail = Label(self.screen1, text ="Email address", width = "25")
        self.labelEmail.place(x=17, y=170)
        self.labelRequired2 = Label(self.screen1, text="*", fg = "red")
        self.labelRequired2.place(x=335, y=170)
        self.emailEntry = Entry(self.screen1, textvariable = self.email)
        self.emailEntry.place(x=170, y=170)

        self.labelPassword = Label(self.screen1, text ="Password", width = "25")
        self.labelPassword.place(x=31, y=210)
        self.labelRequired2 = Label(self.screen1, text="*", fg = "red")
        self.labelRequired2.place(x=335, y=210)
        self.passwordEntry = Entry(self.screen1, textvariable = self.password, show = '*')
        self.passwordEntry.place(x=170, y=210)

        self.labelConfirm = Label(self.screen1, text ="Confirm", width = "25")
        self.labelConfirm.place(x=35, y=250)
        self.labelRequried2 = Label(self.screen1, text="*", fg = "red")
        self.labelRequired2.place(x=335, y=250)
        self.confirmEntry = Entry(self.screen1, textvariable = self.password2, show = '*')
        self.confirmEntry.place(x=170, y=250)

        Button(self.screen1, text="Register", width=20, bg="brown", fg="white", command = self.registrationComplete) .place(x=160, y=490)

    def registrationComplete(self):

        global usernameInfo
        global passwordInfo
        global password2Info
        global emailinfo

        
        self.usernameInfo = self.username.get()
        self.passwordInfo = self.password.get()
        self.password2Info = self.password2.get()
        self.emailInfo = self.email.get()
        while True:
            if self.usernameInfo != '' and self.passwordInfo != '' and self.password2Info != '' and self.emailInfo != '':
                if '@' in self.emailInfo: 
                    if self.password2Info == self.passwordInfo:
                        break
                    else:
                        self.labelTitle = Label(self.screen1, text="                      Passwords don't match                ", fg="red",font=("calibri", 11))
                        self.labelTitle.place(x=40, y=93)
                        return
                else:
                    self.labelTitle = Label(self.screen1, text="Please enter a valid email address", fg="red", font=("calibri", 11))
                    self.labelTitle.place(x=70, y=93)
                    return
            else:
                self.labelTitle = Label(self.screen1, text="           Please fill the required fields", fg="red", font=("calibri", 11))
                self.labelTitle.place(x=70, y=93)
                return

        self.file.close()
        self.file=open(self.usernameInfo, "w")
        self.file.write(self.usernameInfo+"\n")
        self.file.write(self.passwordInfo+"\n")
        self.file.write(self.emailInfo+"\n")

        self.usernameEntry.delete(0, END)
        self.passwordEntry.delete(0, END)
        self.emailEntry.delete(0, END)
        self.confirmEntry.delete(0, END)

        self.labelTitle = Label(self.screen1, text="Registration success, you can now log in with your user", fg="green", font=("calibri", 11))
        self.labelTitle.place(x=50, y=93)

class login(main):
    def __init__(self):
        super().__init__()
        global screen2
        self.screen2 = Toplevel(self.screen)
        self.screen2.title("Login")
        self.screen.withdraw()
        self.screen2.geometry("500x300")
        Label(self.screen2, text="Please enter details below to login").pack()
        Label(self.screen2, text="").pack()

        global usernameVerify
        global passwordVerify

        self.usernameVerify = StringVar()
        self.passwordVerify = StringVar()

        global usernameEntry1
        global passwordEntry1

        Label(self.screen2, text="Username * ").pack()
        self.usernameEntry1 = Entry(self.screen2, textvariable = self.usernameVerify)
        self.usernameEntry1.pack()
        Label(self.screen2, text="").pack()
        Label(self.screen2, text="Password * ").pack()
        self.passwordEntry1 = Entry(self.screen2, textvariable = self.passwordVerify, show = '*')
        self.passwordEntry1.pack()
        Label(self.screen2, text="").pack()
        Button(self.screen2, text = "Login", width = 10, height = 1, command = self.loginVerify).pack()
    def loginVerify(self):
        #super().__init__()
        #global fileList
        #global file1
        self.username1 = self.usernameVerify.get()
        self.password1 = self.passwordVerify.get()
        self.usernameEntry1.delete(0, END)
        self.passwordEntry1.delete(0, END)

        self.fileList = os.listdir()
        while True:
            if self.username1 in self.fileList:
                self.file1 = open(self.username1, "r")
                self.verify = self.file1.read().splitlines()
                if self.password1 in self.verify:
                    self.loginSuccess()
                    break
                else:
                    label_title = Label(self.screen2, text="Invalid username or password", fg="red", font=("calibri", 11))
                    label_title.place(x=135, y=19)
                    return
            else:
                label_title = Label(self.screen2, text="Invalid username or password", fg="red", font=("calibri", 11))
                label_title.place(x=135, y=19)
                return
        
    def loginSuccess(self):
        super().__init__()
        global screen3
        self.screen3 = Toplevel(self.screen)
        self.screen.withdraw()
        self.screen3.title("Logged in")
        self.screen3.geometry("500x300")
        Label(self.screen3, text = "Logged in successfully, \n click OK to access the secret information").pack()
        Button(self.screen3, text = "OK", command = self.access(self)).pack()

    def passwordNotRecognised(self):
        global screen4
        self.screen4 = Toplevel(self.screen)
        self.screen4.title("Error")
        self.screen4.geometry("150x100")
        Label(self.screen4, text = "Password could not be linked to any username").pack()
        Button(self.screen4, text = "OK", command = delete3).pack()

    def userNotFound(self):
        global screen5
        self.screen5 = Toplevel(self.screen)
        self.screen5.title("Error")
        self.screen5.geometry("150x100")
        Label(self.screen5, text = "We could not find given username from our system").pack()
        Button(self.screen5, text = "OK", command = self.delete4).pack()




mainCall = main()
mainCall.run()








    class main(object):
        def __init__(self):
            global screen
            self.screen = Tk()
            self.screen.geometry("500x300")
            self.screen.title("Login and Registration System")
            Label(text = "Welcome!", width = "300", height = "2", font = ("Calibri", 10)).pack()
            Label(text = "If you don't have a user, click on the register button to create an account.", width = "300", height = "2", font = ("Calibri", 10)).pack()
            Button(text = "Login", width = "30", height = "2", font = ("Calibri", 13), command = login).pack()
            Label(text = "").pack()
            Button(text = "Register", width = "30", height = "2", font = ("Calibri", 13), command = register).pack()
            Label(text = "").pack()
            #Button(text = "Guest", width = "30", height = "2", font = ("Calibri", 13), command = self.access).pack()
            #Label(text = "").pack()
        def access(self):
            self.screen3.destroy()
            global screen6
            self.screen6 = Toplevel(self.screen)
            self.screen6.title("Secret information")
            self.screen6.geometry("550x300")
            Label(self.screen6, text = "Welcome to the Mandelbrot Set Fractal visualisation tool").pack()
            Button(self.screen6, text = "exit", command = exit).pack()
        def run(self):
            self.screen.mainloop()
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
Coder123
  • 1
  • 4
  • Could you please provide the code for the `login` and `register` procedures? Also, the rest of the code would be good too. My current guess is that you are calling `.pack()` several times which would continue to pack new buttons into the GUI. – doliphin Nov 15 '20 at 12:14
  • this is not the best way to use multiple pages in Tkinter see https://stackoverflow.com/questions/14817210/using-buttons-in-tkinter-to-navigate-to-different-pages-of-the-application – coderoftheday Nov 15 '20 at 12:15
  • I'm so sorry man this is so embarrassing, it's one of my first times using stackoverflow and my code isn't posting properly @OliverMrMineamationIliffe – Coder123 Nov 15 '20 at 12:19
  • It's fine, don't worry. I'm not sure I am able to replicate your issue though. [what I am seeing](https://i.imgur.com/ugSwZW4.gif) <-- screen recording of my end – doliphin Nov 15 '20 at 12:29
  • @OliverMrMineamationIliffe yes that is what is happening. If you enlarge the very first window with the login and register button, you can see that the buttons are being duplicated every time you press login or register. Also, I was going to post this in a different question, but since this is more convenient. The bigger problem with my code is that when i try to login after registering an account, it's not identifying the details and it says my login details are incorrect. I don't want to be annoying but if you could help me identify the problem there I would be forever grateful. – Coder123 Nov 15 '20 at 12:35
  • You are running `super.__init__()` each time you call `login`, which means that all the labels and buttons in the `main` `__init__` method are packed again. You could separate them into a separate method. – doliphin Nov 15 '20 at 12:38
  • @OliverMrMineamationIliffe ahh yeah that makes a lot more sense now. I'm so dumb kmt. I've now separated out the buttons in a different method but still within the main class. Did you read the other problem I put in that comment? ^^ – Coder123 Nov 15 '20 at 12:42

1 Answers1

0

Here is the code with this issue fixed, as said in a comment, you are calling .pack() on all your widgets again.

from tkinter import *
import os
class main(object):
    def __init__(self):
        global screen
        self.screen = Tk()
        
    def __pack__(self):
        self.screen.geometry("500x300")
        self.screen.title("Login and Registration System")
        Label(text = "Welcome!", width = "300", height = "2", font = ("Calibri", 10)).pack()
        Label(text = "If you don't have a user, click on the register button to create an account.", width = "300", height = "2", font = ("Calibri", 10)).pack()
        Button(text = "Login", width = "30", height = "2", font = ("Calibri", 13), command = login).pack()
        Label(text = "").pack()
        Button(text = "Register", width = "30", height = "2", font = ("Calibri", 13), command = register).pack()
        Label(text = "").pack()
        #Button(text = "Guest", width = "30", height = "2", font = ("Calibri", 13), command = self.access).pack()
        #Label(text = "").pack()


    def access(self):
        self.screen3.destroy()
        global screen6
        self.screen6 = Toplevel(self.screen)
        self.screen6.title("Secret information")
        self.screen6.geometry("550x300")
        Label(self.screen6, text = "Welcome to the Mandelbrot Set Fractal visualisation tool").pack()
        Button(self.screen6, text = "exit", command = exit).pack()
    def run(self):
        self.screen.mainloop()
class destroy():
    '''
    def delete(self):
        self.screen2.destroy()
    def delete2(self):
        self.screen3.destroy()
    '''
    def delete3(self):
        self.screen4.destroy()
    def delete4(self):
        self.screen5.destroy()
    
    
class register(main):
    def __init__(self):
        super().__init__()
        global screen1
        self.screen1 = Toplevel()
        self.screen.withdraw()
        self.screen1.geometry("500x600")
        self.screen1.title("Register")

        global username
        global password
        global email
        global password2
        global usernameEntry
        global passwordEntry
        global emailEntry
        global confirmEntry

        self.username = StringVar()
        self.email = StringVar()
        self.c = StringVar()
        self.password = StringVar()
        self.password2 = StringVar()

        self.labelTitle = Label(self.screen1, text = "Create an account", font = ("Calibri", 20))
        self.labelTitle.place(x=50, y=3)

        self.labelTitle = Label(self.screen1, text = "Please, fill in the details below in order to register")
        self.labelTitle.place(x=90, y=50)
        self.labelTitle = Label(self.screen1, text = "* indicates a required field", fg = "red")
        self.labelTitle.place(x=140, y=73)

        self.labelUser = Label(self.screen1, text ="Username", width = "25")
        self.labelUser.place(x=30, y=130)
        self.labelRequired = Label(self.screen1, text="*", fg = "red")
        self.labelRequired.place(x=335, y=130)
        self.usernameEntry = Entry(self.screen1, textvariable = self.username)
        self.usernameEntry.place(x=170, y=130)

        self.labelEmail = Label(self.screen1, text ="Email address", width = "25")
        self.labelEmail.place(x=17, y=170)
        self.labelRequired2 = Label(self.screen1, text="*", fg = "red")
        self.labelRequired2.place(x=335, y=170)
        self.emailEntry = Entry(self.screen1, textvariable = self.email)
        self.emailEntry.place(x=170, y=170)

        self.labelPassword = Label(self.screen1, text ="Password", width = "25")
        self.labelPassword.place(x=31, y=210)
        self.labelRequired2 = Label(self.screen1, text="*", fg = "red")
        self.labelRequired2.place(x=335, y=210)
        self.passwordEntry = Entry(self.screen1, textvariable = self.password, show = '*')
        self.passwordEntry.place(x=170, y=210)

        self.labelConfirm = Label(self.screen1, text ="Confirm", width = "25")
        self.labelConfirm.place(x=35, y=250)
        self.labelRequried2 = Label(self.screen1, text="*", fg = "red")
        self.labelRequired2.place(x=335, y=250)
        self.confirmEntry = Entry(self.screen1, textvariable = self.password2, show = '*')
        self.confirmEntry.place(x=170, y=250)

        Button(self.screen1, text="Register", width=20, bg="brown", fg="white", command = self.registrationComplete) .place(x=160, y=490)

    def registrationComplete(self):

        global usernameInfo
        global passwordInfo
        global password2Info
        global emailinfo

        
        self.usernameInfo = self.username.get()
        self.passwordInfo = self.password.get()
        self.password2Info = self.password2.get()
        self.emailInfo = self.email.get()
        while True:
            if self.usernameInfo != '' and self.passwordInfo != '' and self.password2Info != '' and self.emailInfo != '':
                if '@' in self.emailInfo: 
                    if self.password2Info == self.passwordInfo:
                        break
                    else:
                        self.labelTitle = Label(self.screen1, text="                      Passwords don't match                ", fg="red",font=("calibri", 11))
                        self.labelTitle.place(x=40, y=93)
                        return
                else:
                    self.labelTitle = Label(self.screen1, text="Please enter a valid email address", fg="red", font=("calibri", 11))
                    self.labelTitle.place(x=70, y=93)
                    return
            else:
                self.labelTitle = Label(self.screen1, text="           Please fill the required fields", fg="red", font=("calibri", 11))
                self.labelTitle.place(x=70, y=93)
                return

        self.file.close()
        self.file=open(self.usernameInfo, "w")
        self.file.write(self.usernameInfo+"\n")
        self.file.write(self.passwordInfo+"\n")
        self.file.write(self.emailInfo+"\n")

        self.usernameEntry.delete(0, END)
        self.passwordEntry.delete(0, END)
        self.emailEntry.delete(0, END)
        self.confirmEntry.delete(0, END)

        self.labelTitle = Label(self.screen1, text="Registration success, you can now log in with your user", fg="green", font=("calibri", 11))
        self.labelTitle.place(x=50, y=93)

class login(main):
    def __init__(self):

        super().__init__()
        global screen2
        self.screen2 = Toplevel(self.screen)
        self.screen2.title("Login")
        self.screen.withdraw()
        self.screen2.geometry("500x300")
        Label(self.screen2, text="Please enter details below to login").pack()
        Label(self.screen2, text="").pack()

        global usernameVerify
        global passwordVerify

        self.usernameVerify = StringVar()
        self.passwordVerify = StringVar()

        global usernameEntry1
        global passwordEntry1

        Label(self.screen2, text="Username * ").pack()
        self.usernameEntry1 = Entry(self.screen2, textvariable = self.usernameVerify)
        self.usernameEntry1.pack()
        Label(self.screen2, text="").pack()
        Label(self.screen2, text="Password * ").pack()
        self.passwordEntry1 = Entry(self.screen2, textvariable = self.passwordVerify, show = '*')
        self.passwordEntry1.pack()
        Label(self.screen2, text="").pack()
        Button(self.screen2, text = "Login", width = 10, height = 1, command = self.loginVerify).pack()
    def loginVerify(self):
        #super().__init__()
        #global fileList
        #global file1
        self.username1 = self.usernameVerify.get()
        self.password1 = self.passwordVerify.get()
        self.usernameEntry1.delete(0, END)
        self.passwordEntry1.delete(0, END)

        self.fileList = os.listdir()
        while True:
            if self.username1 in self.fileList:
                self.file1 = open(self.username1, "r")
                self.verify = self.file1.read().splitlines()
                if self.password1 in self.verify:
                    self.loginSuccess()
                    break
                else:
                    label_title = Label(self.screen2, text="Invalid username or password", fg="red", font=("calibri", 11))
                    label_title.place(x=135, y=19)
                    return
            else:
                label_title = Label(self.screen2, text="Invalid username or password", fg="red", font=("calibri", 11))
                label_title.place(x=135, y=19)
                return
        
    def loginSuccess(self):
        super().__init__()
        global screen3
        self.screen3 = Toplevel(self.screen)
        self.screen.withdraw()
        self.screen3.title("Logged in")
        self.screen3.geometry("500x300")
        Label(self.screen3, text = "Logged in successfully, \n click OK to access the secret information").pack()
        Button(self.screen3, text = "OK", command = self.access(self)).pack()

    def passwordNotRecognised(self):
        global screen4
        self.screen4 = Toplevel(self.screen)
        self.screen4.title("Error")
        self.screen4.geometry("150x100")
        Label(self.screen4, text = "Password could not be linked to any username").pack()
        Button(self.screen4, text = "OK", command = delete3).pack()

    def userNotFound(self):
        global screen5
        self.screen5 = Toplevel(self.screen)
        self.screen5.title("Error")
        self.screen5.geometry("150x100")
        Label(self.screen5, text = "We could not find given username from our system").pack()
        Button(self.screen5, text = "OK", command = self.delete4).pack()




mainCall = main()
mainCall.__pack__()
mainCall.run()





doliphin
  • 752
  • 6
  • 22
  • 1
    Thank you man I appreciate this a lot. I changed it already as soon as you said it, such an obvious problem aha I don't know why I didn't think of that, I've spent my whole week doing that ngl. Did you end up seeing the other problem I put? – Coder123 Nov 15 '20 at 12:46
  • Yes, I have and I can answer that question if you open a new question. It's not the question you asked originally – doliphin Nov 15 '20 at 13:07
  • It won't let me because I can't post more than one question within 90 minutes. Is there another way I can contact you sir? – Coder123 Nov 15 '20 at 13:12
  • yeah, @doliphin#8650 on discord – doliphin Nov 15 '20 at 13:25
  • added, it's a1or1upp – Coder123 Nov 15 '20 at 13:31