Skip to content

Commit 7b81198

Browse files
authored
Create Login-System.py
1 parent 7eb8249 commit 7b81198

1 file changed

Lines changed: 138 additions & 0 deletions

File tree

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
import tkinter as tk
2+
from tkinter import messagebox
3+
import hashlib
4+
import json
5+
import os
6+
7+
USER_FILE = 'users.json'
8+
9+
def load_users():
10+
if os.path.exists(USER_FILE):
11+
with open(USER_FILE, 'r') as f:
12+
return json.load(f)
13+
return {}
14+
15+
def save_users(users):
16+
with open(USER_FILE, 'w') as f:
17+
json.dump(users, f)
18+
19+
def hash_password(password):
20+
return hashlib.sha256(password.encode()).hexdigest()
21+
22+
class LoginApp:
23+
def __init__(self, root):
24+
self.root = root
25+
self.root.title('Modern Login System')
26+
self.root.geometry('450x550')
27+
self.root.configure(bg='#1f2f3a')
28+
self.root.resizable(False, False)
29+
self.users = load_users()
30+
self.create_login_screen()
31+
32+
def clear_screen(self):
33+
for widget in self.root.winfo_children():
34+
widget.destroy()
35+
36+
def create_login_screen(self):
37+
self.clear_screen()
38+
tk.Label(self.root, text='Welcome Back!', font=('Helvetica', 28, 'bold'), bg='#1f2f3a', fg='#ffffff').pack(pady=30)
39+
40+
# Username input
41+
username_frame = tk.Frame(self.root, bg='#1f2f3a')
42+
username_frame.pack(pady=10)
43+
tk.Label(username_frame, text='Username', font=('Helvetica', 12), bg='#1f2f3a', fg='#bdc3c7').pack(anchor='w')
44+
self.username_entry = tk.Entry(username_frame, font=('Helvetica', 14), bd=0, highlightthickness=2, highlightbackground='#2980b9', width=30)
45+
self.username_entry.pack(pady=5, ipady=6) # ipady increases vertical padding inside entry
46+
47+
# Password input
48+
password_frame = tk.Frame(self.root, bg='#1f2f3a')
49+
password_frame.pack(pady=10)
50+
tk.Label(password_frame, text='Password', font=('Helvetica', 12), bg='#1f2f3a', fg='#bdc3c7').pack(anchor='w')
51+
self.password_entry = tk.Entry(password_frame, font=('Helvetica', 14), bd=0, highlightthickness=2, highlightbackground='#2980b9', show='*', width=30)
52+
self.password_entry.pack(pady=5, ipady=6)
53+
54+
# Buttons
55+
btn_frame = tk.Frame(self.root, bg='#1f2f3a')
56+
btn_frame.pack(pady=20)
57+
58+
login_btn = tk.Button(btn_frame, text='Login', command=self.login, bg='#2980b9', fg='white', font=('Helvetica', 12, 'bold'), width=25, height=2, bd=0, activebackground='#3498db', cursor='hand2')
59+
login_btn.pack(pady=5)
60+
register_btn = tk.Button(btn_frame, text='Register', command=self.create_register_screen, bg='#27ae60', fg='white', font=('Helvetica', 12, 'bold'), width=25, height=2, bd=0, activebackground='#2ecc71', cursor='hand2')
61+
register_btn.pack(pady=5)
62+
forgot_btn = tk.Button(btn_frame, text='Forgot Password?', command=self.forgot_password, bg='#e67e22', fg='white', font=('Helvetica', 12, 'bold'), width=25, height=2, bd=0, activebackground='#f39c12', cursor='hand2')
63+
forgot_btn.pack(pady=5)
64+
65+
def create_register_screen(self):
66+
self.clear_screen()
67+
tk.Label(self.root, text='Create Account', font=('Helvetica', 28, 'bold'), bg='#1f2f3a', fg='#ffffff').pack(pady=30)
68+
69+
# Username
70+
username_frame = tk.Frame(self.root, bg='#1f2f3a')
71+
username_frame.pack(pady=10)
72+
tk.Label(username_frame, text='Username', font=('Helvetica', 12), bg='#1f2f3a', fg='#bdc3c7').pack(anchor='w')
73+
self.reg_username = tk.Entry(username_frame, font=('Helvetica', 14), bd=0, highlightthickness=2, highlightbackground='#27ae60', width=30)
74+
self.reg_username.pack(pady=5, ipady=6)
75+
76+
# Password
77+
password_frame = tk.Frame(self.root, bg='#1f2f3a')
78+
password_frame.pack(pady=10)
79+
tk.Label(password_frame, text='Password', font=('Helvetica', 12), bg='#1f2f3a', fg='#bdc3c7').pack(anchor='w')
80+
self.reg_password = tk.Entry(password_frame, font=('Helvetica', 14), bd=0, highlightthickness=2, highlightbackground='#27ae60', show='*', width=30)
81+
self.reg_password.pack(pady=5, ipady=6)
82+
83+
# Confirm Password
84+
confirm_frame = tk.Frame(self.root, bg='#1f2f3a')
85+
confirm_frame.pack(pady=10)
86+
tk.Label(confirm_frame, text='Confirm Password', font=('Helvetica', 12), bg='#1f2f3a', fg='#bdc3c7').pack(anchor='w')
87+
self.reg_confirm = tk.Entry(confirm_frame, font=('Helvetica', 14), bd=0, highlightthickness=2, highlightbackground='#27ae60', show='*', width=30)
88+
self.reg_confirm.pack(pady=5, ipady=6)
89+
90+
# Buttons
91+
btn_frame = tk.Frame(self.root, bg='#1f2f3a')
92+
btn_frame.pack(pady=20)
93+
94+
register_btn = tk.Button(btn_frame, text='Register', command=self.register, bg='#27ae60', fg='white', font=('Helvetica', 12, 'bold'), width=25, height=2, bd=0, activebackground='#2ecc71', cursor='hand2')
95+
register_btn.pack(pady=5)
96+
back_btn = tk.Button(btn_frame, text='Back to Login', command=self.create_login_screen, bg='#c0392b', fg='white', font=('Helvetica', 12, 'bold'), width=25, height=2, bd=0, activebackground='#e74c3c', cursor='hand2')
97+
back_btn.pack(pady=5)
98+
99+
def login(self):
100+
username = self.username_entry.get()
101+
password = self.password_entry.get()
102+
hashed = hash_password(password)
103+
if username in self.users and self.users[username] == hashed:
104+
messagebox.showinfo('Success', f'Welcome {username}!')
105+
else:
106+
messagebox.showerror('Error', 'Invalid username or password')
107+
108+
def register(self):
109+
username = self.reg_username.get()
110+
password = self.reg_password.get()
111+
confirm = self.reg_confirm.get()
112+
113+
if not username or not password:
114+
messagebox.showerror('Error', 'All fields are required')
115+
return
116+
if password != confirm:
117+
messagebox.showerror('Error', 'Passwords do not match')
118+
return
119+
if username in self.users:
120+
messagebox.showerror('Error', 'Username already exists')
121+
return
122+
123+
self.users[username] = hash_password(password)
124+
save_users(self.users)
125+
messagebox.showinfo('Success', 'Registration successful!')
126+
self.create_login_screen()
127+
128+
def forgot_password(self):
129+
username = self.username_entry.get()
130+
if username in self.users:
131+
messagebox.showinfo('Password Reset', f'Password reset link sent to {username} (mock)')
132+
else:
133+
messagebox.showerror('Error', 'Username not found')
134+
135+
if __name__ == '__main__':
136+
root = tk.Tk()
137+
app = LoginApp(root)
138+
root.mainloop()

0 commit comments

Comments
 (0)