You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
91 lines
2.5 KiB
91 lines
2.5 KiB
from tkinter import *
|
|
import pymysql
|
|
|
|
|
|
# 注册功能
|
|
def register():
|
|
username = entry_username.get()
|
|
password = entry_password.get()
|
|
|
|
try:
|
|
cursor.execute("INSERT INTO users (username, password) VALUES (%s, %s)", (username, password))
|
|
con.commit()
|
|
label_message.config(text='注册成功!')
|
|
except Exception as e:
|
|
label_message.config(text='注册失败!')
|
|
print('数据库异常', e)
|
|
|
|
|
|
# 登录功能
|
|
def login():
|
|
global count
|
|
username = entry_username.get()
|
|
password = entry_password.get()
|
|
|
|
cursor.execute("SELECT * FROM users WHERE username = %s AND password = %s", (username, password))
|
|
result = cursor.fetchone()
|
|
|
|
if result:
|
|
label_message.config(text='登录成功!')
|
|
window.destroy()
|
|
import welcome
|
|
else:
|
|
label_message.config(text='账号或密码错误!')
|
|
count += 1
|
|
if count == 3:
|
|
label_message.config(text='登录失败!')
|
|
btn_login.config(state='disabled')
|
|
|
|
|
|
# 创建登录界面窗口
|
|
window = Tk()
|
|
window.title("用户登录")
|
|
screen_width = window.winfo_screenwidth()
|
|
screen_height = window.winfo_screenheight()
|
|
|
|
# 设置窗口大小和位置
|
|
window_width = int(screen_width / 2)
|
|
window_height = int(screen_height / 2)
|
|
x = (screen_width - window_width) // 2
|
|
y = (screen_height - window_height) // 2
|
|
window.geometry("{}x{}+{}+{}".format(window_width, window_height, x, y))
|
|
|
|
# 创建用户名、密码输入框和登录按钮
|
|
label_username = Label(window, text="账号:")
|
|
label_username.pack()
|
|
entry_username = Entry(window)
|
|
entry_username.pack()
|
|
|
|
label_password = Label(window, text="密码:")
|
|
label_password.pack()
|
|
entry_password = Entry(window, show='*')
|
|
entry_password.pack()
|
|
|
|
btn_login = Button(window, text="登录", command=login)
|
|
btn_login.place(x=310, y=90)
|
|
|
|
btn_register = Button(window, text="注册", command=register)
|
|
btn_register.place(x=420, y=90)
|
|
|
|
label_message = Label(window, text="", width=window_width)
|
|
label_message.place(x=0, y=120)
|
|
# 数据库连接部分
|
|
con = pymysql.connect(
|
|
host='localhost',
|
|
user='root',
|
|
password='root',
|
|
database='health',
|
|
charset='utf8mb4',
|
|
autocommit=True
|
|
)
|
|
cursor = con.cursor() # 创建游标对象
|
|
|
|
# 查询用户信息并输出
|
|
cursor.execute("select * from users")
|
|
result = cursor.fetchall()
|
|
print(type(result), result)
|
|
for row in result:
|
|
print(row)
|
|
|
|
# 启动窗口主循环
|
|
window.mainloop() |