Here's my part. I'm making an AI chatbot, just working on the interface of logging in and sketching out stuff. I'm also a beginner in .json so this helped me learn.
I'll also explain it maybe.
First, create a .json
file named anything you'd like. Make sure the file is in the same directory path/folder as the code, or you can import os to do that.
Next import Tkinter. It may or may not be an in-built module but try it and see. Also import JSON, time isn't required but can help.
import tkinter as tk
import json
import time
Next, create a window for all this to happen. Edit the info if necessary.
app = tk.Tk()
app.wm_title("Sign Up or Log in")
app.geometry("500x300")
Add a label if you want.
k = tk.Label(text = "Hello there, I'm Lola! We'll talk soon. Just let me know your credentials!\nJust click on sign up below so that I can identify you!", justify="left")
k.pack()
Add a button for the user to click.
sign_in = tk.Button(text="Sign Up or Log In", command=signin)
sign_in.pack()
We need to define the signing function used above for the button. So before we create the button, we define it. It's a bit long so I'll just explain the general parts.
We first get their details before we check it
def signin():
em = tk.Label(text="Email:")
em.pack()
en1 = tk.Entry(width=50)
en1.pack()
pa = tk.Label(text="Password:")
pa.pack()
en2 = tk.Entry(width=50)
en2.pack()
na = tk.Label(text="Name:")
na.pack()
en3 = tk.Entry(width=50)
en3.pack()
Next, let's define the submit function and create the button. This is where json comes in. We first get the details and store them in a variable like so.
def submit():
email = str(en1.get())
password = str(en2.get())
name = str(en3.get())
login = tk.Label(text="")
Then, we shouldn't forget to read the json file first
with open("info.json", "r") as f:
users = json.load(f)
Now let's do the checking
if email in users:
login.config(text="You already have an account! Click log in please!")
loginn = tk.Button(text = "Log in", command = login)
else:
users[email] = {}
users[email]["Password"] = password
users[email]["Name"] = name
with open("info.json", "w") as f:
json.dump(users, f)
login.config(text=f"You've successfully created an account. Just click on log in below! Credentials:\nEmail: {email}\nPassword: {password}\nName: {name}")
login.pack()
Now, we shall define login. Everything is pretty much similar
def loggin():
email = str(en1.get())
password = str(en2.get())
name = str(en3.get())
login = tk.Label(text="")
with open("info.json", "r") as f:
users = json.load(f)
if not email in users:
login.config(text="You don't have an account, sign up instead!")
else:
passs = users[email]["Password"]
if password != passs:
login.config(text="Wrong credentials. It doesn't match what I've recorded")
else:
login.config(text="Success! You've logged in. Please wait, as the software is still in the early stage of development.\nYou might have to sign up again later. I'll let you know soon.")
login.pack()
loginn = tk.Button(text = "Log in", command = loggin)
loginn.pack()
At the end, this one line of code will determine whether everything is going to work. Make sure to put it in your code at the end.
window.mainloop()
And that is the end, please don't copy this, I worked for 5 hours to understand this. I'm a beginner just like everyone else, but please do not copy this. Use it as an example to understand. Even if you do, please give credit. But mostly, don't.
fill="both"
,fill="x"
,padx=20
andanchor="w"
doing. – Lonnylonslesaunier