Create Account and Login with Python
Tue Feb 13 2024 18:42:13 GMT+0000 (Coordinated Universal Time)
Saved by @jrray
# Import tkinter and json modules import tkinter as tk import json # Create a root window root = tk.Tk() # Set the window title root.title("Welcome") # Set the window size root.geometry("300x200") # Create a function to save the user data to a json file def save_user_data(username, password, filename="Accounts1.json"): # Create a dictionary with the user data user_data = {"username": username, "password": password} # Try to open the file and append the user data try: with open(filename, "r+") as file: # Load the existing data file_data = json.load(file) # Append the new user data file_data["users"].append(user_data) # Seek to the beginning of the file file.seek(0) # Dump the updated data json.dump(file_data, file, indent=4) # If the file does not exist, create a new file with the user data except FileNotFoundError: with open(filename, "w") as file: # Create a dictionary with a list of users file_data = {"users": [user_data]} # Dump the data to the file json.dump(file_data, file, indent=4) # Create a function to open a new window for creating an account def create_account(): # Create a top-level window account_window = tk.Toplevel(root) # Set the window title account_window.title("Create Account") # Set the window size account_window.geometry("300x200") # Create a label for username username_label = tk.Label(account_window, text="Username:") # Create an entry box for username username_entry = tk.Entry(account_window) # Create a label for password password_label = tk.Label(account_window, text="Password:") # Create an entry box for password password_entry = tk.Entry(account_window, show="*") # Create a function to submit the account details def submit_account(): # Get the username and password from the entry boxes username = username_entry.get() password = password_entry.get() # Save the user data to the json file save_user_data(username, password) # Close the account window account_window.destroy() # Create a button to submit the account details submit_button = tk.Button(account_window, text="Submit", command=submit_account) # Place the widgets on the window using grid layout username_label.grid(row=0, column=0, padx=10, pady=10) username_entry.grid(row=0, column=1, padx=10, pady=10) password_label.grid(row=1, column=0, padx=10, pady=10) password_entry.grid(row=1, column=1, padx=10, pady=10) submit_button.grid(row=2, column=1, padx=10, pady=10) # Create a function to open a new window for logging in def login(): # Create a top-level window login_window = tk.Toplevel(root) # Set the window title login_window.title("Login") # Set the window size login_window.geometry("300x200") # Create a label for username username_label = tk.Label(login_window, text="Username:") # Create an entry box for username username_entry = tk.Entry(login_window) # Create a label for password password_label = tk.Label(login_window, text="Password:") # Create an entry box for password password_entry = tk.Entry(login_window, show="*") # Create a function to submit the login details def submit_login(): # Get the username and password from the entry boxes username = username_entry.get() password = password_entry.get() # Try to open the json file and check the user data try: with open("Accounts1.json", "r") as file: # Load the data file_data = json.load(file) # Loop through the users list for user in file_data["users"]: # If the username and password match, login successfully if user["username"] == username and user["password"] == password: # Close the login window login_window.destroy() # Show a message box tk.messagebox.showinfo("Login", "Login successful!") # Break the loop break # If the loop ends without breaking, login failed else: # Show a message box tk.messagebox.showerror("Login", "Invalid username or password!") # If the file does not exist, show a message box except FileNotFoundError: tk.messagebox.showerror("Login", "No accounts found!") # Create a button to submit the login details submit_button = tk.Button(login_window, text="Submit", command=submit_login) # Place the widgets on the window using grid layout username_label.grid(row=0, column=0, padx=10, pady=10) username_entry.grid(row=0, column=1, padx=10, pady=10) password_label.grid(row=1, column=0, padx=10, pady=10) password_entry.grid(row=1, column=1, padx=10, pady=10) submit_button.grid(row=2, column=1, padx=10, pady=10) # Create a label for welcome message welcome_label = tk.Label(root, text="Welcome to the Tkinter GUI!") # Create a button for creating an account create_account_button = tk.Button(root, text="Create Account", command=create_account) # Create a button for logging in login_button = tk.Button(root, text="Login", command=login) # Place the widgets on the window using pack layout welcome_label.pack(padx=10, pady=10) create_account_button.pack(padx=10, pady=10) login_button.pack(padx=10, pady=10) # Start the main loop root.mainloop()
Creates a root window with create account button and Login button and implements the functionality for both
Comments