Merge pull request '1' (#4) from main into Qs

Qs
p3hkxan4o 4 weeks ago
commit 03bad2bc6b

@ -0,0 +1,348 @@
import tkinter as tk
from tkinter import messagebox, ttk
import pymysql
import datetime
import time
class MasterChargeSystem(tk.Tk):
current_username=None
def __init__(self):
super().__init__()
self.login_frame = None
self.register_frame = None
self.comment_frame = None
self.detail_window = None
self.create_login_window()
def register_database(self, username, password):
with pymysql.connect(host='localhost', user='root', password='123456', database='mynewdb') as connection:
cursor = connection.cursor()
sql = "INSERT INTO user1(username, password) VALUES (%s, %s)"
cursor.execute(sql, (username, password))
connection.commit()
def login(self):
global current_username
username = self.username_entry.get().strip()
password = self.password_entry.get().strip()
with pymysql.connect(host='localhost', user='root', password='123456', database='mynewdb') as connection:
cursor = connection.cursor()
current_username=username
sql = "SELECT password FROM user1 WHERE username = %s"
cursor.execute(sql, (username,))
result = cursor.fetchone()
if result:
stored_password = result[0]
if stored_password == password:
messagebox.showinfo("登录", "登陆成功")
self.show_main_window()
else:
messagebox.showerror("登录", "用户名或密码错误")
else:
messagebox.showerror("登录", "用户名不存在")
def register(self):
username = self.username_entry.get().strip()
password = self.password_entry.get().strip()
confirm_password = self.confirm_password_entry.get().strip()
if username and password:
if password == confirm_password:
self.register_database(username, password)
messagebox.showinfo("注册", "注册成功")
self.create_login_window()
else:
messagebox.showerror("注册", "两次输入的密码不匹配")
else:
messagebox.showerror("注册", "用户名或密码为空")
def show_main_window(self):
if self.login_frame:
self.login_frame.pack_forget()
if self.comment_frame:
self.comment_frame.pack_forget()
if self.detail_window:
self.detail_window.pack_forget()
self.title("考研信息系统")
self.attributes('-fullscreen',True)
self.main_frame = tk.Frame(self)
self.main_frame.pack(fill=tk.BOTH, expand=True, padx=50, pady=50)
main_label = tk.Label(self.main_frame, text="欢迎来到考研信息系统!")
main_label.pack(pady=10)
try:
with pymysql.connect(host='localhost', user='root', password='123456', database='mynewdb') as connection:
cursor = connection.cursor()
# 添加搜索框
search_frame = tk.Frame(self.main_frame)
search_frame.pack(pady=10)
search_label = tk.Label(search_frame, text="搜索:地区")
search_label.pack(side=tk.LEFT)
cursor.execute("select distinct 省份 from universities_beijing")
location = cursor.fetchall()
self.search2_entry = ttk.Combobox(search_frame, values=location)
self.search2_entry.pack(side=tk.LEFT, padx=5)
cursor.execute("select distinct 招生年份 from universities_beijing")
year = cursor.fetchall()
search_label = tk.Label(search_frame, text=" 年份")
search_label.pack(side=tk.LEFT)
self.search3_entry = ttk.Combobox(search_frame, values=year)
self.search3_entry.pack(side=tk.LEFT, padx=5)
cursor.execute("select distinct 学校类型 from universities_beijing")
time = cursor.fetchall()
search_label = tk.Label(search_frame, text=" 学校类型")
search_label.pack(side=tk.LEFT)
self.search4_entry = ttk.Combobox(search_frame, values=time)
self.search4_entry.pack(side=tk.LEFT, padx=5)
search_label = tk.Label(search_frame, text=" 院校")
search_label.pack(side=tk.LEFT)
self.search1_entry = tk.Entry(search_frame)
self.search1_entry.pack(side=tk.LEFT, padx=5)
affirm_button = tk.Button(search_frame, text="确认", command=self.search_data)
affirm_button.pack(side=tk.RIGHT)
# self.search_entry.bind("<KeyRelease>", self.search_data) #自动读取键盘输入搜索
# 创建一个用于 Treeview 和滚动条的框架
tree_frame = tk.Frame(self.main_frame)
tree_frame.pack(fill=tk.BOTH, expand=True)
self.tree = ttk.Treeview(tree_frame)
self.vertical_scrollbar = ttk.Scrollbar(tree_frame, orient=tk.VERTICAL, command=self.tree.yview)
self.tree.configure(yscrollcommand=self.vertical_scrollbar.set)
# 获取列名
cursor.execute("SELECT * FROM universities_beijing LIMIT 0") # 替换为您的表名
self.columns = [description[0] for description in cursor.description]
# 获取数据
cursor.execute("SELECT * FROM universities_beijing") # 获取所有数据
self.data = cursor.fetchall()
# 设置列
self.tree['columns'] = self.columns
self.tree.column("#0", width=0, stretch=tk.NO) # 隐藏第一个空列
# 动态设置列和列名
for column in self.columns:
self.tree.column(column, anchor=tk.W, width=50, stretch=tk.YES) # 设置为自适应宽度
self.tree.heading(column, text=column, anchor=tk.W)
# 将 treeview 和滚动条放置到 tree_frame
self.tree.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
self.vertical_scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
# 插入数据
for row in self.data:
self.tree.insert("", tk.END, values=row)
# 绑定点击事件
self.tree.bind("<Double-1>", self.show_university_details)
except pymysql.Error as err:
messagebox.showerror("数据库错误", str(err))
exit_button = tk.Button(self.main_frame, text="退出", command=self.quit)
exit_button.pack(pady=10)
comment_button = tk.Button(self.main_frame, text="评论", command=self.comment)
comment_button.pack(pady=10)
def show_university_details(self, event):
selected_item = self.tree.selection()[0] # 获取选中的行
university_data = self.tree.item(selected_item, 'values') # 获取行数据
with pymysql.connect(host='localhost', user='root', password='123456', database='mynewdb')as connection:
cursor= connection.cursor()
if university_data:
self.detail_window = tk.Toplevel(self) # 创建新窗口
self.detail_window.title("大学详细信息")
self.detail_window.attributes('-fullscreen', True)
cursor.execute("SELECT * FROM universities_beijing LIMIT 0") # 替换为您的表名
columns = [desc[0] for desc in cursor.description]
tree_detail = ttk.Treeview(self.detail_window, columns=[1,2], show='headings')
# 显示大学的其他信息
i=0
while i < len(columns):
for detail in university_data:
tree_detail.insert("","end",values=(columns[i]+":",detail))
i+=1
tree_detail.pack(expand=True, fill='both')
# 添加评论框
tree_comment=ttk.Treeview(self.detail_window,columns=[1,2,3,4],show='headings')
tree_comment.column('1',width=300)
tree_comment.column('2', width=300)
tree_comment.column('3', width=800)
tree_comment.column('4',width=200)
tree_comment.pack(expand=True,fill='both')
sql="select * FROM comments where university_name=%s Order by comment_time desc"
cursor.execute(sql,(university_data[0],))
result = cursor.fetchall()
for row in result:
tree_comment.insert("", tk.END, values=row)
comment_label = tk.Label(self.detail_window, text="添加评论:")
comment_label.pack(pady=10)
self.comment_entry = tk.Entry(self.detail_window)
self.comment_entry.pack(pady=5)
submit_comment_button = tk.Button(self.detail_window, text="提交评论",command=lambda: self.submit_comment(university_data[0]))
submit_comment_button.pack(pady=10)
back_button = tk.Button(self.detail_window, text="返回", command=self.detail_window.destroy)
back_button.pack(pady=10)
cursor.close()
def submit_comment(self, university_name):
global current_username
comment = self.comment_entry.get()
if comment:
# Store comment in the database (add your SQL query here)
with pymysql.connect(host='localhost', user='root', password='123456', database='mynewdb') as connection:
cursor = connection.cursor()
comment_time=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
sql = "INSERT INTO comments (username,university_name, comment,comment_time) VALUES (%s,%s, %s, %s)"
cursor.execute(sql, (current_username,university_name, comment,comment_time))
connection.commit()
messagebox.showinfo("评论", "评论提交成功")
self.comment_entry.delete(0, tk.END) # 清空输入框
else:
messagebox.showerror("评论", "评论不能为空")
def comment(self):
if self.main_frame:
self.main_frame.pack_forget()
self.title("评论界面")
self.attributes('-fullscreen', True)
self.comment_frame=tk.Frame(self)
self.comment_frame.pack(fill=tk.BOTH, expand=True, padx=50, pady=50)
self.back_button = tk.Button(self.comment_frame, text="返回", command=self.show_main_window)
self.back_button.pack(pady=10)
def search_data(self):
search1_term = self.search1_entry.get()
search2_term = self.search2_entry.get()
search3_term = self.search3_entry.get()
search4_term = self.search4_entry.get()
# 清空当前Treeview
self.tree.delete(*self.tree.get_children())
# 重新插入符合搜索条件的数据
for row in self.data:
if any((search1_term in str(value).lower() for value in row)):
if any((search2_term in str(value).lower() for value in row)):
if any((search3_term in str(value).lower() for value in row)):
if any((search4_term in str(value).lower() for value in row)):
self.tree.insert("", tk.END, values=row)
def show_register_window(self):
if self.login_frame:
self.login_frame.pack_forget()
self.title("用户注册界面")
sw = self.winfo_screenwidth()
sh = self.winfo_screenheight()
Width = 400
Height = 300
cen_x = (sw - Width) / 2
cen_y = (sh - Height) / 2
self.geometry('%dx%d+%d+%d' % (Width, Height, cen_x, cen_y))
self.register_frame = tk.Frame(self)
self.register_frame.pack(padx=20, pady=20)
self.username_label = tk.Label(self.register_frame, text="用户名:")
self.username_label.grid(row=0, column=0)
self.username_entry = tk.Entry(self.register_frame)
self.username_entry.grid(row=0, column=1)
self.password_label = tk.Label(self.register_frame, text="密码:")
self.password_label.grid(row=1, column=0)
self.password_entry = tk.Entry(self.register_frame, show="*")
self.password_entry.grid(row=1, column=1)
self.confirm_password_label = tk.Label(self.register_frame, text="再次输入密码:")
self.confirm_password_label.grid(row=2, column=0)
self.confirm_password_entry = tk.Entry(self.register_frame, show="*")
self.confirm_password_entry.grid(row=2, column=1)
self.register_button = tk.Button(self.register_frame, text="注册", command=self.register)
self.register_button.grid(row=3, column=0)
self.back_button = tk.Button(self.register_frame, text="返回", command=self.create_login_window)
self.back_button.grid(row=3, column=1)
def create_login_window(self):
if self.register_frame:
self.register_frame.pack_forget()
self.title("登录界面")
sw = self.winfo_screenwidth()
sh = self.winfo_screenheight()
Width = 400
Height = 300
cen_x = (sw - Width) / 2
cen_y = (sh - Height) / 2
self.geometry('%dx%d+%d+%d' % (Width, Height, cen_x, cen_y))
self.login_frame = tk.Frame(self)
self.login_frame.pack(padx=20, pady=20)
self.username_label = tk.Label(self.login_frame, text="用户名:")
self.username_label.grid(row=0, column=0)
self.username_entry = tk.Entry(self.login_frame)
self.username_entry.grid(row=0, column=1)
self.password_label = tk.Label(self.login_frame, text="密码:")
self.password_label.grid(row=1, column=0)
self.password_entry = tk.Entry(self.login_frame, show="*")
self.password_entry.grid(row=1, column=1)
self.login_button = tk.Button(self.login_frame, text="登录", command=self.login)
self.login_button.grid(row=2, column=0)
self.back_button = tk.Button(self.login_frame, text="退出", command=self.quit)
self.back_button.grid(row=2, column=1)
self.register_button = tk.Button(self.login_frame, text="注册", command=self.show_register_window)
self.register_button.grid(row=3, column=0)
self.deiconify()
# Start the application
if __name__ == "__main__":
app = MasterChargeSystem()
app.mainloop()
Loading…
Cancel
Save