diff --git a/某期刊的在线投稿审稿管理系统/main.py b/某期刊的在线投稿审稿管理系统/main.py index 6cf3f95..8ad91ca 100644 --- a/某期刊的在线投稿审稿管理系统/main.py +++ b/某期刊的在线投稿审稿管理系统/main.py @@ -7,7 +7,7 @@ import tkinter.messagebox as messagebox #弹窗 #主界面 class StartPage: def __init__(self, parent_window): - #parent_window.destroy()#销毁子界面 + # parent_window.destroy()#销毁子界面 self.window = tk.Tk() self.window.title('某期刊的在线投稿审稿管理系统') self.window.geometry('500x600') @@ -19,33 +19,41 @@ class StartPage: height=2,fg='white',bg='gray',activebackground='black',activeforeground='white').pack(pady=25) self.window.mainloop()#主消息循环 def back(self): - #parent_window.destroy() # 销毁子界面 StartPage(self.window) - -#作者账号设置 class Authorlogin: def __init__(self): self.window = tk.Tk() # 初始窗口的声明 self.window.title('请先登录作者账户') self.window.geometry('500x500') + label = tk.Label(self.window, text='作者登录', bg='pink', font=('Verdana', 20), width=30, height=4) label.pack() + # 注意:这里应使用tk.Label而不是label(它是一个已经定义的变量) tk.Label(self.window, text='作者账号:', font=tkFont.Font(size=14)).pack(pady=25) self.author_account = tk.Entry(self.window, font=tkFont.Font(size=14)) self.author_account.pack() + tk.Label(self.window, text='作者密码:', font=tkFont.Font(size=14)).pack(pady=25) self.author_password = tk.Entry(self.window, font=tkFont.Font(size=14), bg='Ivory', show='*') self.author_password.pack() + # 使用lambda表达式传递无参数的函数给command - tk.Button(self.window, text="登录", width=8, font=tkFont.Font(size=12), command=lambda:self.login()).pack( + tk.Button(self.window, text="登录", width=8, font=tkFont.Font(size=12), command=lambda: self.login()).pack( pady=25) + # 您可能还想添加一个“取消”或“返回”按钮 - tk.Button(self.window, text="返回首页", width=8, font=tkFont.Font(size=12), command=lambda :self.back).pack(pady=25) + #tk.Button(self.window, text="返回首页", width=8, font=tkFont.Font(size=12), command=self.back).pack(pady=25) + + # 假设您还没有实现back方法,这里注释掉或添加实现 + # self.window.protocol("WM_DELETE_WINDOW", self.back) + self.window.mainloop() # 进入消息循环 + import pymysql + from tkinter import messagebox def login(self): account = self.author_account.get() @@ -83,13 +91,6 @@ class Authorlogin: else: messagebox.showinfo("Error", "Author not found.") - def back(self): - self.window.destroy() # 销毁窗口,结束Tkinter消息循环 - # 设置WM_DELETE_WINDOW协议以调用back方法 - self.window.protocol("WM_DELETE_WINDOW",self.back()) - - - #审稿人账号设置 class Reviewerlogin: @@ -115,12 +116,16 @@ class Reviewerlogin: pady=25) # 您可能还想添加一个“取消”或“返回”按钮 - tk.Button(self.window, text="返回首页", width=8, font=tkFont.Font(size=12), command=self.back).pack(pady=25) + # tk.Button(self.window, text="返回首页", width=8, font=tkFont.Font(size=12), command=self.back).pack(pady=25) + + # 假设您还没有实现back方法,这里注释掉或添加实现 + # self.window.protocol("WM_DELETE_WINDOW", self.back) self.window.mainloop() # 进入消息循环 def login(self): - account = self.reviewer_account.get() - password_input = self.reviewer_password.get() + print(str(self.reviewer_account.get())) + print(str(self.reviewer_password.get())) + reviewer_password = None # 数据库操作 查询作者表 db = pymysql.connect( host='127.0.0.1', @@ -139,7 +144,7 @@ class Reviewerlogin: # 获取所有记录列表 result = cursor.fetchall() for row in result: - reviewer_account = row[4] + reviewer_account= row[0] reviewer_password = row[5] # 打印结果 print("reviewer_account=%s,reviewer_password=%s" % (reviewer_account, reviewer_password)) @@ -148,313 +153,333 @@ class Reviewerlogin: messagebox.showinfo("Error", "Author not found.") db.close() print("正在登录审稿人界面") - print("self", self.reviewer_name) + print("self", self.reviewer_password) print("local", reviewer_password) if self.reviewer_password.get() == reviewer_password: - Reviewermaanage(self.window) # 进入审稿人操作界面 + Reviewermanage(self.window) # 进入审稿人操作界面 else: messagebox.showinfo("Error", "Author not found.") - def back(self): self.window.destroy() # 销毁窗口,结束Tkinter消息循环 # 设置WM_DELETE_WINDOW协议以调用back方法 self.window.protocol("WM_DELETE_WINDOW", self.back()) - self.window.mainloop() # 进入消息循环 +class Reviewermanage: + def __init__(self, parent_window): + parent_window.destroy() # 销毁主界面 + self.window = tk.Tk() # 初始框的声明 + self.window.title('审稿人操作界面') -#作者操作界面 -class Authormanage: - def __init__(self,parent_window): - parent_window.destroy()#销毁主界面 - self.window=tk.Tk()#初始框的声明 - self.window.title('作者操作界面') - self.frame_left_top=tk.Frame(height=200,width=300).pack(pady=10) - self.frame_right_top=tk.Frame(height=200,width=200).pack(pady=10) - self.frame_center=tk.Frame(height=400,width=500).pack(pady=10) - self.frame_bottom=tk.Frame(height=50,width=650).pack(pady=10) - #定义下方中心列表区域 - self.columns=("编号","内容","类型","标题") - self.tree=ttk.Treeview(self.frame_center) - self.vbar=ttk.Scrollbar(self.frame_center,orient=tk.VERTICAL) - self.vbar.pack(side=tk.RIGHT,fill=tk.Y) - #定义树形结构与滚动条 - self.tree.configure(yscrollcommand=self.vbar.set) - #表格的标题 - self.tree.column("编号",width=150,anchor='center') - self.tree.column("内容",width=150,anchor='center') - self.tree.column("类型",width=100,anchor='center') - self.tree.column("标题",width=100,anchor='center') - #调用方法获取表格内容插入 - self.tree.grid(row=0,column=0,sticky=NSEW) - self.vbar.grid(row=0,column=1,sticky=NS) - self.manuscript_id=[] - self.manuscript_content=[] - self.manuscript_type=[] - self.manuscript_title=[] - #打开数据库连接 - db=pymysql.connect( + label = tk.Label(self.window, text='审稿人操作界面', bg='green', font=('Verdana', 20), width=30, height=4) + label.pack() + + # 注意:这里应使用tk.Label而不是label(它是一个已经定义的变量) + tk.Label(self.window, text='稿件编号:', font=tkFont.Font(size=14)).pack(pady=25) + self.manuscript_id = tk.Entry(self.window, font=tkFont.Font(size=14)) + self.manuscript_id.pack() + + # 使用lambda表达式传递无参数的函数给command + tk.Button(self.window, text="查看", width=8, font=tkFont.Font(size=12), command=lambda: self.yanzheng()).pack( + pady=25) + def yanzheng(self): + manuscript_id = self.manuscript_id.get() + self.window.destroy() + # 数据库操作 查询作者表 + db = pymysql.connect( host='127.0.0.1', port=3306, user='root', passwd='123456', database='投稿', charset='utf8' + # cursorclass=pymysql.cursors.DictCursor # 游标类型默认为元组,现在我们设置为字典类型 ) - cursor=db.cursor() - sql="SELECT * FROM manuscript"#查询稿件表 + cursor = db.cursor() # 使用cursor()方法获取操作游标 + sql = "select * from manuscript where manuscript_id='%s'" % self.manuscript_id.get() try: - #执行sql语句 + # 执行sql语句 cursor.execute(sql) - #获取所有记录列表 - result=cursor.fetchall() + # 获取所有记录列表 + result = cursor.fetchall() for row in result: - self.manuscript_id.append(row[0]) - self.manuscript_content.append(row[1]) - self.manuscript_type.append(row[2]) - self.manuscript_title.append(row[3]) + manuscript_id = row[0] + # 打印结果 + print("manuscript_id=%s" % (manuscript_id)) except: - print("Error:unable to fetch data") - messagebox.showinfo('警告!','数据库连接失败!') + print("Error:unable to fecth date") + messagebox.showinfo("Error", "manuscript_id not found.") db.close() - print("test*********************************************") - for i in range(len(min(len(self.manuscript_id))),len(self.manuscript_content),len(self.manuscript_type),len(self.manuscript_title)): - self.tree.insert('',i, - values=(self.manuscript_id[i], - self.manuscript_content[i], - self.manuscript_type[i], - self.manuscript_title[i])) - for col in self.columns:#绑定函数,使表头可排序 - self.tree.heading(col, text=col, - command=lambda _col=col:self.tree_sort_column(self.tree,_col)) - #定义顶部区域和定义左上方区域 - self.top_title=Label(self.frame_left_top,text="稿件信息:",font=('Verdana',20)) - self.top_title.grid(row=0,column=0,sticky=NSEW,columnspan=2,padx=50,pady=10) - self.left_top_frame=tk.Frame(self.frame_left_top) - self.var_manuscript_id=StringVar()#声明稿件编号 - self.var_manuscript_content=StringVar() - self.var_manuscript_type=StringVar() - self.var_manuscript_title=StringVar() - #稿件编号 - self.right_top_manuscript_id_label=Label(self.frame_left_top,text="编号:",font=('Verdana',15)) - self.right_top_manuscript_id_entry=Entry(self.frame_right_top,textvariable=self.var_manuscript_id,font=tkFont.Font(size=14)) - self.right_top_manuscript_id_label.grid(row=1,column=0)#位置设置 - self.right_top_manuscript_id_entry.grid(row=1,column=1) - #稿件内容 - self.right_top_manuscript_content_label = Label(self.frame_left_top, text="内容:", font=('Verdana', 15)) - self.right_top_manuscript_content_entry = Entry(self.frame_right_top, textvariable=self.var_manuscript_content,font=tkFont.Font(size=14)) - self.right_top_manuscript_content_label.grid(row=2, column=0) # 位置设置 - self.right_top_manuscript_content_entry.grid(row=2, column=1) - #稿件类型 - self.right_top_manuscript_type_label = Label(self.frame_left_top, text="类型:", font=('Verdana', 15)) - self.right_top_manuscript_type_entry = Entry(self.frame_right_top, textvariable=self.var_manuscript_type, - font=tkFont.Font(size=14)) - self.right_top_manuscript_type_label.grid(row=3, column=0) # 位置设置 - self.right_top_manuscript_type_entry.grid(row=3, column=1) - #稿件标题 - self.right_top_manuscript_title_label = Label(self.frame_left_top, text="标题:", font=('Verdana', 15)) - self.right_top_manuscript_title_entry = Entry(self.frame_right_top, textvariable=self.var_manuscript_title, - font=tkFont.Font(size=14)) - self.right_top_manuscript_title_label.grid(row=4, column=0) # 位置设置 - self.right_top_manuscript_title_entry.grid(row=4, column=1) - #定义右上方区域 - self.right_top_title=Label(self.frame_right_top,text="操作:",font=('Verdana', 20)) - self.tree.bind('',self.click)#左键获取 - self.right_top_button1=ttk.Button(self.frame_right_top,text='添加',width=20,command=self.new_row) - self.right_top_button1 = ttk.Button(self.frame_right_top, text='更新', width=20, command=self.update_row) - self.right_top_button1 = ttk.Button(self.frame_right_top, text='删除', width=20, command=self.del_row) - #位置设置 - self.right_top_title.grid(row=1, column=0,pady=10) - self.right_top_button1.grid(row=2,column=0,pady=10, padx=10) - self.right_top_button2.grid(row=3, column=0, pady=10, padx=10) - self.right_top_button3.grid(row=4, column=0, pady=10, padx=10) - #整体区域定位 - self.frame_left_top.grid(row=0,column=0,columnspan=2,padx=2,pady=5) - self.frame_right_top.grid(row=0,column=1,columnspan=2,padx=30,pady=30) - self.frame_center.grid(row=1,column=0,columnspan=2,padx=4,pady=5) - self.frame_bottom.grid(row=1,column=0,columnspan=2) - self.frame_left_top.grid_propagate(0) - self.frame_right_top.grid_propagate(0) - self.frame_center.grid_propagate(0) - self.frame_bottom.grid_propagate(0) - self.frame_left_top.tkraise()#开始显示主菜单 - self.frame_right_top.tkraise() - self.frame_center.tkraise() - self.frame_bottom.tkraise() - self.window.protocol("WM_DELETE_WINDOW",self.back)#捕捉右上方关闭点击 - def back(self): - StartPage(self.window)#显示主窗口 销毁本窗口 - def click(self,event): - self.col=self.tree.identify_column(event.x)#列 - self.row=self.tree.identify_row(event.y)#行 - print(self.col) - print(self.row) - self.row_info=self.tree.item(self.row,"values") - self.var_manuscript_id.set(self.row_info[0]) - self.var_manuscript_content.set(self.row_info[1]) - self.var_manuscript_type.set(self.row_info[2]) - self.var_manuscript_title.set(self.row_info[3]) - self.right_top_manuscript_id_entry=Entry(self.frame_left_top,textvariable=self.var_manuscript_id,font=tkFont.Font(size=14)) - print('') - - def tree_sort_column_by_id(self, tv, col, reverse=False): - # 假设id是Treeview条目中的一个值,并且我们想要根据这个id进行排序 - def get_id(manuscript_id): - # 获取条目的id值,这里假设id是Treeview条目的一个值 - return int(tv.item(manuscript_id, 'values')[manuscript_id]) # index_of_id是id在values中的索引 - - # 获取所有子项的id和它们的id-item_id对 - items = [(get_id(k), k) for k in tv.get_children('')] - - # 根据id进行排序 - items.sort(key=lambda x: x[0], reverse=reverse) - - # 清空Treeview以重新插入排序后的条目(可选,取决于你的需求) - # for item in tv.get_children(''): - # tv.delete(item) - - # 根据排序后的顺序重新插入或移动条目 - for index, (id_value, item_id) in enumerate(items): - # 如果Treeview是空的或者我们需要重新插入所有条目,则使用insert - # 否则,只使用move方法移动现有的条目 - if index == 0: - # 第一个条目或者Treeview是空的,使用insert - tv.insert('', 'end', item_id, values=tv.item(item_id, 'values')) - else: - # 其他条目,使用move - prev_item_id = items[index - 1][1] - tv.move(item_id, '', tv.index(prev_item_id) + 1) # +1是因为index是从0开始的,但Treeview的索引是从1开始的 - - # 更新列标题的排序箭头和点击事件(如果需要) - def sort_command(): - self.tree_sort_column_by_id(tv, col, not reverse) - - tv.heading(col, text=col, command=sort_command) - - # 注意:你需要知道id在values中的索引,这里用index_of_id表示 - # 例如,如果你的Treeview条目的values是(id, name, date),那么index_of_id就是0 + print("正在登录查看界面") + print("local", manuscript_id) + if self.manuscript_id.get() == manuscript_id: + ViewWindow(tk.Toplevel) # 进入查看稿件界面 + else: + messagebox.showinfo("Error", "manuscript_id not found.") - def update_row(self): - res=messagebox.askyesnocancel('警告!','是否更新所填写的数据?') - if res==True: - if self.var_manuscript_id.get()==self.row_info[0]:#如果所填编号和所选编号相同则打开数据库 - db = pymysql.connect( - host='127.0.0.1', - port=3306, - user='root', - passwd='123456', - database='投稿', - charset='utf8', - # cursorclass=pymysql.cursors.DictCursor # 游标类型默认为元组,现在我们设置为字典类型 - ) - db=db.cursor() - sql_update="UPDATE manuscript SET manuscript_content=%s,manuscript_type=%s,manuscript_title=%s WHERE manuscript_id=%s"%(self.var_manuscript_content.get(),self.var_manuscript_type.get(),self.var_manuscript_title.get(),self.var_manuscript_id) - try: - cursor = db.cursor() - cursor.execute(sql_update) - db.commit() - messagebox.showinfo('提交!','更新成功!') - except: - db.rollback() - messagebox.showinfo('警告!','更新失败,数据库连接失败!') - db.close() - id_index=self.manuscript_id.index(self.row_info[0]) - self.manuscript_content[id_index]=self.var_manuscript_content.get() - self.manuscript_type[id_index]=self.var_manuscript_type.get() - self.manuscript_title[id_index]=self.var_manuscript_title.get() - self.tree.item(self.tree.selection()[0],values=( - self.var_manuscript_id.get(),self.var_manuscript_content.get(),self.manuscript_type.get(),self.manuscript_title.get()))#修改对于行信息 - else: - messagebox.showinfo('警告!','不能修改稿件编号') - def del_row(self): - res=messagebox.askyesnocancel('警告!','是否删除所选数据?') - if res==True: - print(self.row_info[0])#鼠标选中的编号 - print(self.tree.selection()[0])#行号 - print(self.tree.get_children())#所有行 - db = pymysql.connect( - host='127.0.0.1', - port=3306, - user='root', - passwd='123456', - database='投稿', - charset='utf8', - # cursorclass=pymysql.cursors.DictCursor # 游标类型默认为元组,现在我们设置为字典类型 - ) - cursor = db.cursor() - sql_delete="DELETE FROM manuscript WHERE manuscript_id=%s"%(self.row_info[0]) - #sql_deletes="DELETE FROM stu_login_k WHERE manuscript_id=%s"%(self.row_info[0]) - try: - cursor.execute(sql_delete) - #cursor.execute(sql_deletes) - db.commit() - messagebox.showinfo('提示!','删除成功!') - except: - db.rollback() - messagebox.showinfo('警告!','删除失败,数据库连接失败!') - db.close() - id_index=self.manuscript_id.index(self.row_info[0]) - print(id_index) - del self.manuscript_id[id_index] - del self.manuscript_content[id_index] - del self.manuscript_type[id_index] - del self.manuscript_title[id_index] - print(self.manuscript_id) - self.tree.delete(self.tree.selection()[0])#删除所选行 - print(self.tree.get_children()) - -#审稿人审稿界面 -class Reviewermaanage: - def __init__(self,parent_window,manuscript_id): - parent_window.destroy()#销毁主界面 - self.window=tk.Tk()#初始框的声明 - self.window.title('查看稿件') - self.window.geometry('300x480') - label=tk.Label(self.window,text='稿件查看',bg='silver',font=('Verdana',20),width=20,height=2) - label.pack(pady=20) - self.manuscript_id='编号:'+'' - self.manuscript_content='内容:'+'' - self.manuscript_type='类型:'+'' - self.manuscript_title='标题'+'' +class ViewWindow: + def __init__(self, parent_window): + parent_window.destroy() # 销毁主界面 + self.window = tk.Tk() # 初始框的声明 + self.window.title('查看界面') + self.window.mainloop() + def chakan(self): + manuscript_id = self.manuscript_id.get() + self.window.destroy() + # 数据库操作 查询作者表 db = pymysql.connect( host='127.0.0.1', port=3306, user='root', passwd='123456', database='投稿', - charset='utf8', + charset='utf8' # cursorclass=pymysql.cursors.DictCursor # 游标类型默认为元组,现在我们设置为字典类型 ) - cursor = db.cursor() - sql="SELECT * FROM manuscript WHERE manuscript_id='%$'"%(manuscript_id) + cursor = db.cursor() # 使用cursor()方法获取操作游标 + sql = "select * from manuscript where manuscript_id='%s'" % self.manuscript_id.get() try: + # 执行sql语句 cursor.execute(sql) - result=cursor.fetchall() + # 获取所有记录列表 + result = cursor.fetchall() for row in result: - self.manuscript_id = '编号:' +row[0] - self.manuscript_content = '内容:' + row[1] - self.manuscript_type = '类型:' + row[2] - self.manuscript_title = '标题' + row[3] + manuscript_id = row[0] + # 打印结果 + print("manuscript_id=%s" % (manuscript_id)) except: - print("Error:unable to fetch data") + print("Error:unable to fecth date") + messagebox.showinfo("Error", "manuscript_id not found.") db.close() - Label(self.window,text=self.manuscript_id,font=('Verdana',18)).pack(pady=5) - Label(self.window,text=self.manuscript_content,font=('Verdana',18)).pack(pady=5) - Label(self.window,text=self.manuscript_type,font=('Verdana',18)).pack(pady=5) - Label(self.window,text=self.manuscript_title,font=('Verdana',18)).pack(pady=5) - Button(self.window,text="通过",width=8,font=tkFont.Font(size=16)).pack(pady=25) - Button(self.window,text="不通过",width=8,font=tkFont.Font(size=16)).pack(pady=25) - Button(self.window,text="返回首页",width=8,font=tkFont.Font(size=16),command=self.back).pack(pady=25) - self.window.protocol("WM_DELETE_WINDOW",self.back) - self.window.mainloop() + + # 显示信息的函数 + + def show_window(self): + result = self.manuscript_id.get() + if result: + # 假设result是一个元组,例如(id, name, description) + label_id = tk.Label(self.window, text=f"稿件编号: {result[0]}") + label_name = tk.Label(self.window, text=f"稿件内容: {result[1]}") + label_desc = tk.Label(self.window, text=f"稿件类型: {result[2]}") + label_desc = tk.Label(self.window, text=f"稿件标题: {result[2]}") + + label_id.pack(pady=5) + label_name.pack(pady=5) + label_desc.pack(pady=5) + + # 主窗口和按钮 + + + +class Authormanage: + def __init__(self, parent_window): + parent_window.destroy() # 销毁主界面 + self.window = tk.Tk() # 初始框的声明 + self.window.title('作者操作界面') + self.row_info = [] + self.current_manuscript_id = None + label = tk.Label(self.window, text='作者操作界面', bg='pink', font=('Verdana', 20), width=30, height=4) + label.pack() + + # 注意:这里应使用tk.Label而不是label(它是一个已经定义的变量) + tk.Label(self.window, text='稿件编号:', font=tkFont.Font(size=14)).pack(pady=25) + self.manuscript_id = tk.Entry(self.window, font=tkFont.Font(size=14)) + self.manuscript_id.pack() + + tk.Label(self.window, text='稿件内容:', font=tkFont.Font(size=14)).pack(pady=25) + self.manuscript_content = tk.Entry(self.window, font=tkFont.Font(size=14)) + self.manuscript_content.pack() + tk.Label(self.window, text='稿件类型:', font=tkFont.Font(size=14)).pack(pady=25) + self.manuscript_type = tk.Entry(self.window, font=tkFont.Font(size=14)) + self.manuscript_type.pack() + tk.Label(self.window, text='稿件标题:', font=tkFont.Font(size=14)).pack(pady=25) + self.manuscript_title = tk.Entry(self.window, font=tkFont.Font(size=14)) + self.manuscript_title.pack() + + # 使用lambda表达式传递无参数的函数给command + tk.Button(self.window, text="添加", width=8, font=tkFont.Font(size=12), command=self.new_row).pack(pady=25) + tk.Button(self.window, text="更新", width=8, font=tkFont.Font(size=12), command=lambda: self. update_row()).pack( + pady=25) + tk.Button(self.window, text="删除", width=8, font=tkFont.Font(size=12), command=lambda: self.del_row(self.current_manuscript_id)).pack( + pady=25) + def back(self): - StartPage(self.window) -# 使用 StartPage 类(这里假设没有父窗口,所以传入 None) -if __name__ == "__main__": - StartPage(None) + self.window.destroy() # 销毁窗口,结束Tkinter消息循环 + # 设置WM_DELETE_WINDOW协议以调用back方法 + self.window.protocol("WM_DELETE_WINDOW", self.back()) + self.window.mainloop() # 进入消息 + + def new_row(self): + manuscript_id = self.manuscript_id.get().strip() # 假设这是用户输入的新ID + # 检查重复的ID + if manuscript_id in self.manuscript_id: + messagebox.showinfo("警告", "该稿件编号已存在!") + return + + # 检查其他字段是否为空 + if not all(( + self.manuscript_content.get().strip(), + self.manuscript_type.get().strip(), + self.manuscript_title.get().strip() + )): + messagebox.showinfo("错误", "所有字段都必须填写!") + return + + try: + db = pymysql.connect( + host='127.0.0.1', + port=3306, + user='root', + passwd='123456', + database='投稿', + charset='utf8' + ) + with db.cursor() as cursor: + sql = "INSERT INTO manuscript(manuscript_id, manuscript_content, manuscript_type, manuscript_title) VALUES(%s, %s, %s, %s)" + cursor.execute(sql, ( + manuscript_id, + self.manuscript_content.get(), + self.manuscript_type.get(), + self.manuscript_title.get() + )) + db.commit() + messagebox.showinfo("成功", "新记录已添加!") + except pymysql.MySQLError as e: + messagebox.showerror("错误", str(e)) + finally: + if db: + db.close() -#class Review: - #def __init__(self,window): + def update_row(self): + res = messagebox.askyesnocancel('警告!', '是否更新所填写的数据?') + if res is None: # 用户点击了取消 + return + if res: # 如果用户同意 + # 直接从文本框获取manuscript_id + manuscript_id = self.manuscript_id.get() + if manuscript_id: # 确保有输入 + try: + db = pymysql.connect( + host='127.0.0.1', + port=3306, + user='root', + passwd='123456', # 注意:硬编码的密码是不安全的 + database='投稿', + charset='utf8' + ) + cursor = db.cursor() + + # 使用参数化查询来避免SQL注入 + sql_update = "UPDATE manuscript SET manuscript_content=%s, manuscript_type=%s, manuscript_title=%s WHERE manuscript_id=%s" + update_data = ( + self.manuscript_content.get(), + self.manuscript_type.get(), + self.manuscript_title.get(), + manuscript_id + ) + cursor.execute(sql_update, update_data) + db.commit() + messagebox.showinfo('提交!', '更新成功!') + except pymysql.MySQLError as e: + # 处理数据库错误 + messagebox.showerror('错误', str(e)) + finally: + # 关闭数据库连接 + db.close() + + def del_row(self, manuscript_id): + self.row_info = [row for row in self.row_info if row['manuscript_id'] != manuscript_id] + res = messagebox.askyesnocancel('警告!', '是否删除所选数据?') + if res: # 用户点击了是 + manuscript_id = self.row_info # 假设row_info包含要删除的行的信息 + + # 连接到数据库 + db = pymysql.connect( + host='127.0.0.1', + port=3306, + user='root', + passwd='123456', # 注意:硬编码的密码是不安全的 + database='投稿', + charset='utf8' + ) + cursor = db.cursor() + + # 使用参数化查询来避免SQL注入 + sql_delete = "DELETE FROM manuscript WHERE manuscript_id,manuscript_content,manuscript_type,manuscript_title=%s,%s,%s,%s" + try: + cursor.execute(sql_delete, (manuscript_id,)) + db.commit() + messagebox.showinfo('提示!', '删除成功!') + + # 更新本地列表(移除与Treeview相关的部分) + id_index = self.manuscript_id.index(manuscript_id) + del self.manuscript_id[id_index] + del self.manuscript_content[id_index] + del self.manuscript_type[id_index] + del self.manuscript_title[id_index] + + except pymysql.MySQLError as e: + db.rollback() + messagebox.showerror('错误!', f'删除数据时出错: {e}') + except Exception as e: # 捕获其他可能的异常 + messagebox.showerror('错误!', f'发生未知错误: {e}') + finally: + db.close() # 确保关闭数据库连接 + + def remove_manuscript(self, manuscript_id): + try: + # 查找 manuscript_id 的索引 + id_index = self.manuscript_id.index(manuscript_id) + + # 从所有相关列表中删除元素 + del self.manuscript_id[id_index] + del self.manuscript_content[id_index] + del self.manuscript_type[id_index] + del self.manuscript_title[id_index] + + # 如果你的 Treeview 控件与这些列表相关,你可能还需要更新它 + # ... 更新 Treeview 的代码 ... + + except ValueError: + # 如果 manuscript_id 不在列表中,打印一个消息或执行其他操作 + print(f"Manuscript ID {manuscript_id} not found in the list.") + + def load_data_from_database(self): + # 连接到数据库 + db = pymysql.connect( + host='127.0.0.1', + port=3306, + user='root', + passwd='123456', # 注意:硬编码的密码是不安全的 + database='投稿', + charset='utf8' + ) + cursor = db.cursor() + + # 执行SQL查询 + sql_select = "SELECT manuscript_id,manuscript_content, manuscript_type, manuscript_title FROM manuscript" + try: + cursor.execute(sql_select) + results = cursor.fetchall() # 获取所有结果 + + # 将结果转换为所需的列表格式 + for row in results: + manuscript_id, content, type_, title = row + self.row_info.append({ + 'manuscript_id': manuscript_id, + 'manuscript_content': content, + 'manuscript_type': type_, + 'manuscript_title': title + }) + + except pymysql.MySQLError as e: + messagebox.showerror('错误!', f'查询数据时出错: {e}') + except Exception as e: + messagebox.showerror('错误!', f'发生未知错误: {e}') + finally: + db.close() # 确保关闭数据库连接 +if __name__ == "__main__": + StartPage(None)