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.

164 lines
5.9 KiB

from tkinter import *
import tkinter as tk
import pymysql
from PIL import Image, ImageTk
from tkinter import messagebox
conn = pymysql.connect(host='localhost',
user='root',
password='15616242572jd',
database='user',
port=3306,
charset='utf8')
# 使用 cursor() 方法创建一个游标对象 cursor
cursor = conn.cursor()
# 使用 execute() 方法执行 SQL 查询
cursor.execute("select 账号 from user ")
# 使用 fetchone() 方法获取单条数据.
data = cursor.fetchall()
print("数据库连接成功!")
zh = []
for sublist in data:
for item in sublist:
zh.append(item)
cursor.execute('select 密码 from user')
# 获取查询结果
mm = []
data2 = cursor.fetchall()
for sublist in data2:
for item in sublist:
mm.append(item)
def register():
deng = Tk()
deng.title("注册")
deng.geometry("300x200")
label_new_username = Label(deng, text="新账号:")
label_new_username.pack()
entry_new_username = Entry(deng)
entry_new_username.pack()
label_new_password = Label(deng, text="新密码:")
label_new_password.pack()
entry_new_password = Entry(deng, show="*")
entry_new_password.pack()
btn_register = Button(deng, text="注册", command=lambda: register_action(entry_new_username, entry_new_password, deng))
btn_register.pack(padx=50)
deng.mainloop()
def register_action(entry_new_username, entry_new_password, deng):
new_username = entry_new_username.get()
new_password = entry_new_password.get()
if new_username in zh:
messagebox.showwarning('注册失败', '账号已存在!')
else:
cursor.execute("INSERT INTO user (账号, 密码) VALUES (%s, %s)", (new_username, new_password))
conn.commit()
messagebox.showinfo('注册成功', '注册成功')
deng.destroy()
count = 0
def login():
global count
username = entry_username.get()
password = entry_password.get()
# 使用 execute() 方法执行 SQL 查询
cursor.execute("select 账号 from user ")
# 使用 fetchone() 方法获取单条数据.
data3 = cursor.fetchall()
z = []
for sublist in data3:
for item in sublist:
z.append(item)
cursor.execute('select 密码 from user')
# 获取查询结果
m = []
data4 = cursor.fetchall()
for sublist in data4:
for item in sublist:
m.append(item)
if username not in z:
messagebox.showwarning('登录失败', '账号错误!')
elif password not in m:
messagebox.showwarning('登录失败', '密码错误还有{}次机会!'.format(3-count))
count += 1
if count == 3:
messagebox.showwarning('登录失败', '登录失败')
btn_login.config(state='disabled')
else:
messagebox.showinfo('登录成功', '登录成功!')
if __name__ == "__main__":
windows.destroy()
app = user_jiemian()
app.mainloop()
class user_jiemian(tk.Tk):
def __init__(self):
super().__init__()
self.title('用户界面')
self.geometry('300x300')
tk.Label(self, text='请选择你的功能').pack()
tk.Button(self, text="查看费用分布情况", command=self.windows2).pack()
tk.Button(self, text="查看费用与日期的关系", command=self.windows3).pack()
tk.Button(self, text="查看旅游天数的分布情况", command=self.windows4).pack()
def windows2(self):
self.clear()
self.title("查看费用分布情况")
self.geometry("650x650")
self.back_button = tk.Button(self, text="返回", command=self.return_back)
self.back_button.pack()
image = Image.open("2.png")
photo = ImageTk.PhotoImage(image)
label = tk.Label(self, image=photo)
label.image = photo
label.pack()
self.back_button.pack()
def windows3(self):
self.clear()
self.title("查看费用与日期的关系")
self.geometry("650x650")
self.back_button = tk.Button(self, text="返回", command=self.return_back)
self.back_button.pack()
image = Image.open("1.png")
photo = ImageTk.PhotoImage(image)
label = tk.Label(self, image=photo)
label.image = photo
label.pack()
def windows4(self):
self.clear()
self.title("查看旅游天数的分布情况")
self.geometry("650x650")
self.back_button = tk.Button(self, text="返回", command=self.return_back)
self.back_button.pack()
image = Image.open("3.png")
photo = ImageTk.PhotoImage(image)
label = tk.Label(self, image=photo)
label.image = photo
label.pack()
def clear(self):
for tuhua in self.winfo_children():
tuhua.destroy()
def return_back(self):
self.clear()
self.title('用户界面')
self.geometry('300x300')
tk.Label(self, text='请选择你的功能').pack()
tk.Button(self, text="查看费用分布情况", command=self.windows2).pack()
tk.Button(self, text="查看费用与日期的关系", command=self.windows3).pack()
tk.Button(self, text="查看旅游天数的分布情况", command=self.windows4).pack()
windows = Tk()
windows.title("登录")
windows.geometry("300x200")
label_username = Label(windows, text="账号:")
label_username.pack()
entry_username = Entry(windows)
entry_username.pack()
label_password = Label(windows, text="密码:")
label_password.pack()
entry_password = Entry(windows, show="*")
entry_password.pack()
btn_login = Button(windows, text="登录", command=login)
btn_login.pack(side='left', padx=50)
entry_password.pack()
btn_login = Button(windows, text='注册', command=register)
btn_login.pack(side='right', padx=50)
label_message = Label(windows, text="")
label_message.pack()
windows.mainloop()