diff --git a/Python程序设计课程设计报告.doc b/Python程序设计课程设计报告.doc index f32b6de..6e67636 100644 Binary files a/Python程序设计课程设计报告.doc and b/Python程序设计课程设计报告.doc differ diff --git a/TR/TrialRecommend/LoginPage.py b/TR/TrialRecommend/LoginPage.py index 9992ee9..971e29a 100644 --- a/TR/TrialRecommend/LoginPage.py +++ b/TR/TrialRecommend/LoginPage.py @@ -2,6 +2,7 @@ import tkinter as tk from tkinter import messagebox from TrialRecommend.LoginCheck import login_check from Main import Mainpage +import mysql.connector class Login_page(): @@ -19,11 +20,41 @@ class Login_page(): tk.Entry(self.page,textvariable=self.username).grid(row=1, column=2) tk.Label(self.page, text='密码:').grid(row=2, column=1,pady=10) tk.Entry(self.page,textvariable=self.password,show='*').grid(row=2, column=2) - tk.Button(self.page,text='登录',command=self.check).grid(row=3, column=1,pady=10) - tk.Button(self.page,text='退出',command=self.page.quit).grid(row=3, column=3,pady=10) + tk.Button(self.page,text='登录',command=self.check,width=10,fg='green').grid(row=3, column=1,pady=10) + tk.Button(self.page,text='注册',width=10,command=self.register).grid(row=3, column=2,pady=10) + tk.Button(self.page,text='退出',command=self.page.quit,width=10,fg='red').grid(row=3, column=3,pady=10) self.center_window() # 调用函数来居中窗口 self.root.title('旅游推荐系统登录界面') + def register(self): + if self.username.get() == '' or self.password.get() == '': + messagebox.showinfo(message='用户名或密码不能为空') + else: + db = mysql.connector.connect( + host="localhost", + user="root", + passwd="A121023020309a", + database="trial" + ) + + sql = 'INSERT INTO trial.user (id, psw) VALUES (%s, %s)' + cursor = db.cursor() + success = 'true' + try: + cursor.execute(sql, (str(self.username.get()), str(self.password.get()))) + db.commit() + except db.Error as e: + print(f"An error occurred: {e}") + db.rollback() + messagebox.showinfo("Error", "注册失败!") + success = 'false' + finally: + # 确保cursor关闭,释放资源 + cursor.close() + if success == 'true': + messagebox.showinfo('提示', '注册成功!') + + def check(self): name = self.username.get() psw = self.password.get() diff --git a/TR/TrialRecommend/Main.py b/TR/TrialRecommend/Main.py index 96501b9..638a0c7 100644 --- a/TR/TrialRecommend/Main.py +++ b/TR/TrialRecommend/Main.py @@ -10,6 +10,7 @@ from PIL import Image, ImageTk from TrialRecommend import DataRequest, Notes_Request import func_collec +import dis_collect class Mainpage: def __init__(self,master,name): self.content = '' @@ -73,7 +74,7 @@ class Mainpage: self.btn_liked = tk.Button(self.page, text='收藏攻略', bg='#D7EFFF',font=('SimHei',15,'bold'),command=self.func_collect) self.btn_liked.place(relx=0.9, rely=0.2, relheight=0.05, relwidth=0.1) - self.btn_dis_liked = tk.Button(self.page, text='取消收藏', bg='#D7EFFF',font=('SimHei',15,'bold')) + self.btn_dis_liked = tk.Button(self.page, text='取消收藏', bg='#D7EFFF',font=('SimHei',15,'bold'),command=self.func_discollect) self.btn_dis_liked.place(relx=0.9, rely=0.3, relheight=0.05, relwidth=0.1) self.lable_like=tk.Label(self.page,text='喜欢人数:'+self.likes,font=('SimHei',15,'bold')) @@ -390,6 +391,9 @@ class Mainpage: else: messagebox.showinfo('提示','请先搜索') + def func_discollect(self): + dis_collect.dis_collec(self.user,self.current_data_index) + if __name__ == '__main__': root = tk.Tk() Mainpage(master=root,name='Null') diff --git a/TR/TrialRecommend/__pycache__/DataRequest.cpython-312.pyc b/TR/TrialRecommend/__pycache__/DataRequest.cpython-312.pyc index 567898c..dd144ec 100644 Binary files a/TR/TrialRecommend/__pycache__/DataRequest.cpython-312.pyc and b/TR/TrialRecommend/__pycache__/DataRequest.cpython-312.pyc differ diff --git a/TR/TrialRecommend/__pycache__/Main.cpython-312.pyc b/TR/TrialRecommend/__pycache__/Main.cpython-312.pyc index e61384b..0411919 100644 Binary files a/TR/TrialRecommend/__pycache__/Main.cpython-312.pyc and b/TR/TrialRecommend/__pycache__/Main.cpython-312.pyc differ diff --git a/TR/TrialRecommend/__pycache__/dis_collect.cpython-312.pyc b/TR/TrialRecommend/__pycache__/dis_collect.cpython-312.pyc new file mode 100644 index 0000000..d7add3d Binary files /dev/null and b/TR/TrialRecommend/__pycache__/dis_collect.cpython-312.pyc differ diff --git a/TR/TrialRecommend/__pycache__/func_collec.cpython-312.pyc b/TR/TrialRecommend/__pycache__/func_collec.cpython-312.pyc index 1204368..b5433c1 100644 Binary files a/TR/TrialRecommend/__pycache__/func_collec.cpython-312.pyc and b/TR/TrialRecommend/__pycache__/func_collec.cpython-312.pyc differ diff --git a/TR/TrialRecommend/__pycache__/register.cpython-312.pyc b/TR/TrialRecommend/__pycache__/register.cpython-312.pyc new file mode 100644 index 0000000..4e36d6e Binary files /dev/null and b/TR/TrialRecommend/__pycache__/register.cpython-312.pyc differ diff --git a/TR/TrialRecommend/dis_collect.py b/TR/TrialRecommend/dis_collect.py new file mode 100644 index 0000000..8ff4140 --- /dev/null +++ b/TR/TrialRecommend/dis_collect.py @@ -0,0 +1,36 @@ +import csv +import mysql.connector +from tkinter import messagebox + +def dis_collec(user,data_index): + id='' + with open('TrialRecommend/data.csv', 'r', encoding='utf-8', newline='') as file: + reader = csv.reader(file) + index=0 + for row in reader: + if index == data_index: + id=row[0] + index=index+1 + db = mysql.connector.connect( + host="localhost", + user="root", + passwd="A121023020309a", + database="trial" + ) + + sql='DELETE FROM collections WHERE user_id = %s AND essay_id = %s' + cursor=db.cursor() + success='true' + try: + cursor.execute(sql, (user, id)) + db.commit() + except db.Error as e: + print(f"An error occurred: {e}") + db.rollback() + messagebox.showinfo("Error", "取消收藏失败!") + success='false' + finally: + # 确保cursor关闭,释放资源 + cursor.close() + if success == 'true': + messagebox.showinfo('提示','取消收藏成功!') \ No newline at end of file diff --git a/TR/TrialRecommend/func_collec.py b/TR/TrialRecommend/func_collec.py index d22f3e1..c2caa13 100644 --- a/TR/TrialRecommend/func_collec.py +++ b/TR/TrialRecommend/func_collec.py @@ -33,4 +33,5 @@ def collec(user,data_index): # 确保cursor关闭,释放资源 cursor.close() if success == 'true': - messagebox.showinfo('提示','收藏成功!') \ No newline at end of file + messagebox.showinfo('提示','收藏成功!') + diff --git a/TR/TrialRecommend/register.py b/TR/TrialRecommend/register.py new file mode 100644 index 0000000..7172d00 --- /dev/null +++ b/TR/TrialRecommend/register.py @@ -0,0 +1,62 @@ +import tkinter as tk +from tkinter import messagebox +import mysql.connector +class Register(): + def __init__(self): + self.page = tk.Tk() + self.username=tk.StringVar() + self.password=tk.StringVar() + tk.Label(self.page).grid(row=0, column=0) + tk.Label(self.page, text='账户:').grid(row=1, column=1, pady=10) + tk.Entry(self.page, textvariable=self.username).grid(row=1, column=2) + tk.Label(self.page, text='密码:').grid(row=2, column=1, pady=10) + tk.Entry(self.page, textvariable=self.password).grid(row=2, column=2) + tk.Button(self.page, text='确定', command=self.check, width=10, fg='green').grid(row=3, column=1, pady=10) + tk.Button(self.page, text='退出', command=self.quit, width=10, fg='red').grid(row=3, column=3, pady=10) + self.center_window() # 调用函数来居中窗口 + self.page.title('旅游推荐系统注册界面') + self.page.mainloop() + + def quit(self): + self.page.destroy() + + def check(self): + if self.username.get() == '' or self.password.get() == '': + messagebox.showinfo(message='用户名或密码不能为空') + else: + db = mysql.connector.connect( + host="localhost", + user="root", + passwd="A121023020309a", + database="trial" + ) + + sql = 'INSERT INTO trial.user (id, psw) VALUES (%s, %s)' + cursor = db.cursor() + success = 'true' + try: + cursor.execute(sql, (str(self.username.get()), str(self.password.get()))) + db.commit() + except db.Error as e: + print(f"An error occurred: {e}") + db.rollback() + messagebox.showinfo("Error", "注册失败!") + success = 'false' + finally: + # 确保cursor关闭,释放资源 + cursor.close() + if success == 'true': + messagebox.showinfo('提示', '注册成功!') + self.page.destroy() + + def center_window(self, width=400, height=200): + # 获取屏幕宽度和高度 + screen_width = self.page.winfo_screenwidth() + screen_height = self.page.winfo_screenheight() + + # 计算窗口的左上角应该放置的位置 + left = (screen_width / 2) - (width / 2) + top = (screen_height / 2) - (height / 2) + + # 设置窗口的位置和大小 + self.page.geometry("{0}x{1}+{2}+{3}".format(width, height, int(left), int(top)))