|
|
@ -0,0 +1,241 @@
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
self.about_frame = tk.Frame(self.root)
|
|
|
|
|
|
|
|
tk.Label(self.about_frame, text="本程序由tkinter开发").pack()
|
|
|
|
|
|
|
|
tk.Label(self.about_frame, text="开发者:计科2104谢皓年").pack()
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
import tkinter as tk
|
|
|
|
|
|
|
|
from tkinter import ttk, messagebox
|
|
|
|
|
|
|
|
import MysqlUtil
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AboutFrame(tk.Frame):
|
|
|
|
|
|
|
|
def __init__(self, root):
|
|
|
|
|
|
|
|
super().__init__(master=root)
|
|
|
|
|
|
|
|
tk.Label(self, text="--关于--", font=('', 15), fg='orange').grid(
|
|
|
|
|
|
|
|
row=0, columnspan=2, sticky=tk.EW, pady=5
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
tk.Label(self, text="本程序由tkinter开发", font=('', 15), fg='blue').grid(
|
|
|
|
|
|
|
|
row=1, columnspan=2, sticky=tk.EW, pady=10
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
tk.Label(self, text="开发者:计科2104谢皓年", font=('', 15), fg='blue').grid(
|
|
|
|
|
|
|
|
row=2, columnspan=2, sticky=tk.EW, pady=10
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AddFrame(tk.Frame):
|
|
|
|
|
|
|
|
def __init__(self, root):
|
|
|
|
|
|
|
|
super().__init__(root)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
values = ('已发布', '草稿')
|
|
|
|
|
|
|
|
tk.Label(self, text="").grid(row=0, column=0)
|
|
|
|
|
|
|
|
tk.Label(self, text="--添加文章--", font=('', 15), fg='blue').grid(
|
|
|
|
|
|
|
|
row=0, columnspan=2, sticky=tk.EW, pady=5
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
self.title = tk.StringVar()
|
|
|
|
|
|
|
|
self.author = tk.StringVar()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tk.Label(self, text="标 题:", font=('', 10)).grid(row=1, column=0)
|
|
|
|
|
|
|
|
tk.Entry(self, width=40, textvariable=self.title).grid(row=1, column=1, pady=5)
|
|
|
|
|
|
|
|
tk.Label(self, text="作 者:", font=('', 10)).grid(row=2, column=0)
|
|
|
|
|
|
|
|
tk.Entry(self, width=40, textvariable=self.author).grid(row=2, column=1, pady=5)
|
|
|
|
|
|
|
|
tk.Label(self, text="内 容:", font=('', 10)).grid(row=3, column=0, pady=5)
|
|
|
|
|
|
|
|
self.txt = tk.Text(self, width=40)
|
|
|
|
|
|
|
|
self.txt.grid(row=3, column=1)
|
|
|
|
|
|
|
|
tk.Label(self, text="状 态:", font=('', 10)).grid(row=4, column=0, pady=5)
|
|
|
|
|
|
|
|
self.cbox = ttk.Combobox(self, values=values)
|
|
|
|
|
|
|
|
self.cbox.grid(row=4, column=1, sticky=tk.W, pady=10)
|
|
|
|
|
|
|
|
ttk.Button(self, text='清 空', command=self.clear).grid(row=5, column=1, pady=5, sticky=tk.E)
|
|
|
|
|
|
|
|
ttk.Button(self, text='添 加', command=self.add).grid(row=5, column=1, pady=5)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def add(self):
|
|
|
|
|
|
|
|
self.tit = self.title.get()
|
|
|
|
|
|
|
|
self.authr = self.author.get()
|
|
|
|
|
|
|
|
self.state = self.cbox.get()
|
|
|
|
|
|
|
|
self.content = self.txt.get('1.0', tk.END)
|
|
|
|
|
|
|
|
if self.state != '' and self.content != '' and self.title != '' and self.author != '':
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
MysqlUtil.connect_to_mysql()
|
|
|
|
|
|
|
|
sql = ("insert into article (title, author, content, state) "
|
|
|
|
|
|
|
|
"VALUES ('{}','{}','{}','{}')"
|
|
|
|
|
|
|
|
.format(self.tit, self.authr, self.content, self.state))
|
|
|
|
|
|
|
|
MysqlUtil.not_query_sql(sql)
|
|
|
|
|
|
|
|
messagebox.showinfo(title="提示", message="添加成功")
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
|
|
messagebox.showerror(title="错误", message="添加失败!\n状态只能为“已发布”或“草稿”\n{}".format(e))
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
messagebox.showerror(title="错误", message="请输入完整的数据")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def clear(self):
|
|
|
|
|
|
|
|
self.title.set('')
|
|
|
|
|
|
|
|
self.author.set('')
|
|
|
|
|
|
|
|
self.txt.delete('1.0', tk.END)
|
|
|
|
|
|
|
|
self.cbox.delete(0, tk.END)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ChangeFrame(tk.Frame):
|
|
|
|
|
|
|
|
def __init__(self, root):
|
|
|
|
|
|
|
|
super().__init__(root)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
values = ('已发布', '草稿')
|
|
|
|
|
|
|
|
tk.Label(self, text="").grid(row=0, column=0)
|
|
|
|
|
|
|
|
tk.Label(self, text="--修改文章--", font=('', 15), fg='purple').grid(
|
|
|
|
|
|
|
|
row=0, columnspan=2, sticky=tk.EW, pady=5
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
tk.Label(self, text="编 号:", font=('', 10)).grid(row=1, column=0, pady=5)
|
|
|
|
|
|
|
|
self.id_entry = tk.Entry(self, width=15)
|
|
|
|
|
|
|
|
self.id_entry.grid(row=1, column=1, sticky=tk.W, pady=5)
|
|
|
|
|
|
|
|
ttk.Button(self, text="查 询", command=self.search).grid(row=1, column=1, sticky=tk.E, padx=5)
|
|
|
|
|
|
|
|
self.tip = tk.Label(self, text="", font=('', 10))
|
|
|
|
|
|
|
|
self.tip.grid(row=1, column=1, padx=5)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.title = tk.StringVar()
|
|
|
|
|
|
|
|
self.author = tk.StringVar()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tk.Label(self, text="标 题:", font=('', 10)).grid(row=3, column=0)
|
|
|
|
|
|
|
|
tk.Entry(self, width=40, textvariable=self.title).grid(row=3, column=1, pady=5)
|
|
|
|
|
|
|
|
tk.Label(self, text="作 者:", font=('', 10)).grid(row=4, column=0)
|
|
|
|
|
|
|
|
tk.Entry(self, width=40, textvariable=self.author).grid(row=4, column=1, pady=5)
|
|
|
|
|
|
|
|
tk.Label(self, text="内 容:", font=('', 10)).grid(row=5, column=0, pady=5)
|
|
|
|
|
|
|
|
self.txt = tk.Text(self, width=40)
|
|
|
|
|
|
|
|
self.txt.grid(row=5, column=1)
|
|
|
|
|
|
|
|
tk.Label(self, text="状 态:", font=('', 10)).grid(row=6, column=0, pady=5)
|
|
|
|
|
|
|
|
self.cbox = ttk.Combobox(self, values=values)
|
|
|
|
|
|
|
|
self.cbox.grid(row=6, column=1, sticky=tk.W, pady=10)
|
|
|
|
|
|
|
|
ttk.Button(self, text='清 空', command=self.clear).grid(row=7, column=1, pady=5)
|
|
|
|
|
|
|
|
self.show_save = ttk.Button(self, text='保 存', command=self.save, state='disable')
|
|
|
|
|
|
|
|
self.show_save.grid(row=7, column=1, pady=5, sticky=tk.E)
|
|
|
|
|
|
|
|
# 需要查询后才能修改,这个变量用来存放查询后的id
|
|
|
|
|
|
|
|
self.r_id = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def clear(self):
|
|
|
|
|
|
|
|
self.title.set('')
|
|
|
|
|
|
|
|
self.author.set('')
|
|
|
|
|
|
|
|
self.txt.delete('1.0', tk.END)
|
|
|
|
|
|
|
|
self.cbox.delete(0, tk.END)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def save(self):
|
|
|
|
|
|
|
|
self.tit = self.title.get()
|
|
|
|
|
|
|
|
self.authr = self.author.get()
|
|
|
|
|
|
|
|
self.state = self.cbox.get()
|
|
|
|
|
|
|
|
self.content = self.txt.get('1.0', tk.END)
|
|
|
|
|
|
|
|
if self.r_id != '' and self.tit != "" and self.authr != "" and self.state != "" and self.content != "":
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
MysqlUtil.connect_to_mysql()
|
|
|
|
|
|
|
|
MysqlUtil.not_query_sql("update article set title='{}', author='{}', content='{}'"
|
|
|
|
|
|
|
|
", state='{}' where id='{}'"
|
|
|
|
|
|
|
|
.format(self.tit, self.authr, self.content, self.state, self.r_id))
|
|
|
|
|
|
|
|
messagebox.showinfo("提示", "保存成功")
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
|
|
messagebox.showerror("错误", "保存失败:{}".format(e))
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
messagebox.showerror("错误","需输入完整的数据才能保存")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def search(self):
|
|
|
|
|
|
|
|
t_id = self.id_entry.get().strip()
|
|
|
|
|
|
|
|
if t_id != '':
|
|
|
|
|
|
|
|
MysqlUtil.connect_to_mysql()
|
|
|
|
|
|
|
|
results = MysqlUtil.query_sql("select * from article where id = " + t_id)
|
|
|
|
|
|
|
|
# 查询到非空
|
|
|
|
|
|
|
|
if len(results) > 0:
|
|
|
|
|
|
|
|
self.tip["text"] = "该文章存在"
|
|
|
|
|
|
|
|
self.tip["fg"] = 'green'
|
|
|
|
|
|
|
|
self.show_save.config(state='normal')
|
|
|
|
|
|
|
|
self.r_id = t_id
|
|
|
|
|
|
|
|
# 获取当前编号的文章数据
|
|
|
|
|
|
|
|
result = results[0]
|
|
|
|
|
|
|
|
# 回显该文章
|
|
|
|
|
|
|
|
self.title.set(result[1])
|
|
|
|
|
|
|
|
self.author.set(result[2])
|
|
|
|
|
|
|
|
# 先删除再添加,只能这么修改
|
|
|
|
|
|
|
|
self.txt.delete('1.0', tk.END)
|
|
|
|
|
|
|
|
self.txt.insert(tk.END, result[3])
|
|
|
|
|
|
|
|
self.cbox.set(result[4])
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
self.tip["text"] = "该文章不存在"
|
|
|
|
|
|
|
|
self.tip["fg"] = 'red'
|
|
|
|
|
|
|
|
self.show_save.config(state='disable')
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
messagebox.showerror("错误", "请输入文章编号")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SearchFrame(tk.Frame):
|
|
|
|
|
|
|
|
def __init__(self, root):
|
|
|
|
|
|
|
|
super().__init__(root)
|
|
|
|
|
|
|
|
self.table_view = tk.Frame()
|
|
|
|
|
|
|
|
self.table_view.pack()
|
|
|
|
|
|
|
|
self.create_page()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def create_page(self):
|
|
|
|
|
|
|
|
# tk.Label(self, text="").grid(row=0, column=0)
|
|
|
|
|
|
|
|
tk.Label(self, text="--查询文章--", font=('', 15), fg='brown').pack(
|
|
|
|
|
|
|
|
anchor=tk.CENTER, pady=5
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
columns = ('id', 'title', 'author', 'content', 'state')
|
|
|
|
|
|
|
|
columns_values = ('编号', '标题', '作者', '内容', '状态')
|
|
|
|
|
|
|
|
self.tree_view = ttk.Treeview(self, columns=columns, show='headings')
|
|
|
|
|
|
|
|
# 通过索引遍历元组每一个元素,并渲染在treeview上
|
|
|
|
|
|
|
|
for i in range(len(columns)):
|
|
|
|
|
|
|
|
self.tree_view.column(columns[i], width=95, anchor='center')
|
|
|
|
|
|
|
|
self.tree_view.heading(columns[i], text=columns_values[i])
|
|
|
|
|
|
|
|
self.tree_view.pack(fill='both', expand=True, pady=5)
|
|
|
|
|
|
|
|
self.show_data()
|
|
|
|
|
|
|
|
ttk.Button(self, text='刷新', command=self.show_data).pack(anchor='e', pady=5)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def show_data(self):
|
|
|
|
|
|
|
|
# 删除treeview所有的数据
|
|
|
|
|
|
|
|
for _ in map(self.tree_view.delete, self.tree_view.get_children('')):
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MysqlUtil.connect_to_mysql()
|
|
|
|
|
|
|
|
# 获取最新的数据
|
|
|
|
|
|
|
|
results = MysqlUtil.query_sql("select * from article")
|
|
|
|
|
|
|
|
# 将获取的数据遍历,并插入到treeview
|
|
|
|
|
|
|
|
for result in results:
|
|
|
|
|
|
|
|
self.tree_view.insert('', 'end', values=result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DeleteFrame(tk.Frame):
|
|
|
|
|
|
|
|
def __init__(self, root):
|
|
|
|
|
|
|
|
super().__init__(root)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tk.Label(self, text="").grid(row=0, column=0)
|
|
|
|
|
|
|
|
tk.Label(self, text="--删除文章--", font=('', 15), fg='red').grid(
|
|
|
|
|
|
|
|
row=0, columnspan=2, sticky=tk.EW, pady=5
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
self.id = tk.StringVar()
|
|
|
|
|
|
|
|
tk.Label(self, text="编 号:", font=('', 10)).grid(row=1, column=0, pady=5)
|
|
|
|
|
|
|
|
tk.Entry(self, width=10, textvariable=self.id).grid(row=1, column=1, pady=5)
|
|
|
|
|
|
|
|
ttk.Button(self, text="查 询", command=self.search).grid(row=2, column=1, sticky=tk.W, padx=5)
|
|
|
|
|
|
|
|
self.tip = tk.Label(self, text="", font=('', 10))
|
|
|
|
|
|
|
|
self.tip.grid(row=2, column=0)
|
|
|
|
|
|
|
|
self.show_delete = ttk.Button(self, text="删 除", command=self.delete, state='disabled')
|
|
|
|
|
|
|
|
self.show_delete.grid(row=3, column=1, sticky=tk.W, padx=5)
|
|
|
|
|
|
|
|
# 需要查询后才能删除,这个变量用来存放查询后的id,否则entry是啥内容删的就是啥
|
|
|
|
|
|
|
|
self.r_id = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def search(self):
|
|
|
|
|
|
|
|
t_id = self.id.get().strip()
|
|
|
|
|
|
|
|
if t_id != '':
|
|
|
|
|
|
|
|
MysqlUtil.connect_to_mysql()
|
|
|
|
|
|
|
|
results = MysqlUtil.query_sql("select * from article where id = " + t_id)
|
|
|
|
|
|
|
|
# 查询到非空
|
|
|
|
|
|
|
|
if len(results) > 0:
|
|
|
|
|
|
|
|
self.tip["text"] = "该文章存在"
|
|
|
|
|
|
|
|
self.tip["fg"] = 'green'
|
|
|
|
|
|
|
|
self.show_delete.config(state='normal')
|
|
|
|
|
|
|
|
self.r_id = t_id
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
self.tip["text"] = "该文章不存在"
|
|
|
|
|
|
|
|
self.tip["fg"] = 'red'
|
|
|
|
|
|
|
|
self.show_delete.config(state='disable')
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
messagebox.showerror("错误", "请输入文章编号")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def delete(self):
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
MysqlUtil.connect_to_mysql()
|
|
|
|
|
|
|
|
MysqlUtil.not_query_sql("delete from article where id = " + self.r_id)
|
|
|
|
|
|
|
|
messagebox.showinfo("提示", "编号为{}的文章已删除".format(self.r_id))
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
|
|
messagebox.showerror("错误", "删除失败:{}".format(e))
|