-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
130 lines (107 loc) · 4.94 KB
/
app.py
File metadata and controls
130 lines (107 loc) · 4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import tkinter as tk
from tkinter import messagebox
import json
# Функция для загрузки данных пользователей из JSON файла
def load_users():
try:
with open("users.json", "r") as file:
return json.load(file)
except FileNotFoundError:
return {}
# Функция для сохранения данных новых пользователей в JSON файл
def save_users(users):
with open("users.json", "w") as file:
json.dump(users, file)
# Присвоение данных пользователей в словарь
users = load_users()
# Функция для открытия окна регистрации
def open_registration_window():
registration_window = tk.Toplevel(window)
registration_window.title("Registration")
registration_window.geometry("300x200")
registration_window.configure(bg="#F0F0F0")
# Метки и поля ввода для формы регистрации
label_name = tk.Label(registration_window, text="FullName:", bg="#F0F0F0")
label_name.pack()
entry_name = tk.Entry(registration_window)
entry_name.pack()
label_age = tk.Label(registration_window, text="Age:", bg="#F0F0F0")
label_age.pack()
entry_age = tk.Entry(registration_window)
entry_age.pack()
label_username = tk.Label(registration_window, text="Username:", bg="#F0F0F0")
label_username.pack()
entry_username = tk.Entry(registration_window)
entry_username.pack()
label_password = tk.Label(registration_window, text="Password:", bg="#F0F0F0")
label_password.pack()
entry_password = tk.Entry(registration_window, show="*")
entry_password.pack()
# Функция для регистрации нового пользователя
def register():
name = entry_name.get()
age = entry_age.get()
username = entry_username.get()
password = entry_password.get()
if name and age and username and password:
if username in users:
messagebox.showerror("Error", "The username already exists!")
else:
users[username] = {
'name': name,
'age': age,
'password': password
}
messagebox.showinfo("Success", "Registration was successful!")
registration_window.destroy()
else:
messagebox.showerror("Error", "Please fill in all the fields!")
btn_register = tk.Button(registration_window, text="Register", command=register, width=15, bg="#007BFF", fg="white")
btn_register.pack(pady=10)
# Функция для открытия окна входа
def open_login_window():
login_window = tk.Toplevel(window)
login_window.title("Login")
login_window.geometry("300x150")
login_window.configure(bg="#F0F0F0")
label_username = tk.Label(login_window, text="Username:", bg="#F0F0F0")
label_username.pack()
entry_username = tk.Entry(login_window)
entry_username.pack()
label_password = tk.Label(login_window, text="Password:", bg="#F0F0F0")
label_password.pack()
entry_password = tk.Entry(login_window, show="*")
entry_password.pack()
# Функция для выполнения входа
def login():
username = entry_username.get()
password = entry_password.get()
if username and password:
if username in users and users[username]['password'] == password:
messagebox.showinfo("Success", "Successfully logged in!")
login_window.destroy()
else:
messagebox.showerror("Error", "Invalid username or password!")
else:
messagebox.showerror("Error", "Please enter both your username and password!")
btn_login = tk.Button(login_window, text="Login", command=login, width=15, bg="#DC3545", fg="white")
btn_login.pack(pady=10)
# Функция для выхода из программы
def exit_program():
save_users(users)
window.destroy()
# Создание главного окна Tkinter
window = tk.Tk()
window.title("Login & Registration System")
window.geometry("300x150")
window.configure(bg="#F0F0F0")
# Кнопки для открытия окон регистрации и входа
btn_registration = tk.Button(window, text="Registration", command=open_registration_window, width=15, bg="#28A745", fg="white")
btn_registration.pack(pady=10)
btn_login = tk.Button(window, text="Login", command=open_login_window, width=15, bg="#007BFF", fg="white")
btn_login.pack(pady=10)
# Кнопка для выхода из программы
btn_exit = tk.Button(window, text="Exit", command=exit_program, width=15, bg="#6C757D", fg="white")
btn_exit.pack(pady=10)
# Запуск цикла событий Tkinter
window.mainloop()