I working on a project in Python creating a GUI application. Here is a part of my code. My problem is that once the user registers his username and his password it does not save the data into a file. So, once the user wants to log in it says user not found. I can't figure out what is missing it would be very nice if someone could give me a hand. Thanks
class Register(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.config(bg = "black")
def register_user():
username_info = username.get()
password_info = password.get()
username_entry.delete(0, END)
password_entry.delete(0, END)
file = open(username_info, "w")
file.write(username_info "\n")
file.write(password_info "\n")
file.close()
Label(register_user, text="Registration Success", fg="black", font=("calibri", 12)).pack()
Label(self,text="Please enter details below to Register", bg="white").pack()
Label(self,text="").pack()
username = Label(self, text="Username: ", bg="black", fg="white")
username.pack()
entryusername = Entry(self, width=20, bg="white")
entryusername.pack()
pw = Label(self, text="Password: ", bg="black", fg="white")
pw.pack()
entrypw = Entry(self, width=20, bg="white")
entrypw.pack()
save = Button(self, text="Save", width=8, command=lambda: master.switch(Login))
save.pack(padx=10, pady=10)
CodePudding user response:
You never run register_user() so it can't save it.
Maybe in Button you should use command=register_user and at the end of register_user() add master.switch(Login)
BTW:
You get values from wrong widgets. You get username.get() but username is a Label. You should use entryusername.get(). And you get password.get() but you don't have password - you should use entrypw.get(). You also try to delete text in password_entry but you don't have password_entry.
You also use register_user as parent for Label - it is not correct. Besides after registering it will switch to other Frame so it will have no time to display it.
Minimal working code with other changes. But I skip code which change to Login.
I put register_user() as normal function in class so I need to use self. but this makes code more readable.
import tkinter as tk # PEP8: `import *` is not preferred
class Register(tk.Frame):
def __init__(self, master):
super().__init__(master)
self.config(bg="black")
l = tk.Label(self, text="Please enter details below to Register", bg="white")
l.pack(pady=(0, 10))
l = tk.Label(self, text="Username: ", bg="black", fg="white")
l.pack()
self.username_entry = tk.Entry(self, width=20, bg="white")
self.username_entry.pack()
l = tk.Label(self, text="Password: ", bg="black", fg="white")
l.pack()
self.password_entry = tk.Entry(self, width=20, bg="white")
self.password_entry.pack()
b = tk.Button(self, text="Save", width=8, command=self.register_user)
b.pack(padx=10, pady=10)
def register_user(self):
username_info = self.username_entry.get()
password_info = self.password_entry.get()
self.username_entry.delete(0, 'end')
self.password_entry.delete(0, 'end')
file = open(username_info, "w")
file.write(username_info "\n")
file.write(password_info "\n")
file.close()
self.master.switch(Login)
# --- main ---
root = tk.Tk()
reg = Register(root)
reg.pack()
root.mainloop()
