diff --git a/views.py b/views.py new file mode 100644 index 0000000..aab585b --- /dev/null +++ b/views.py @@ -0,0 +1,128 @@ +import tkinter as tk +from tkinter import ttk +from db import db + + +class AboutFrame(tk.Frame): + def __init__(self, root): + super().__init__(root) + tk.Label(self, text='关于作品:管理系统').pack() + tk.Label(self, text='关于作者:胡凯翔').pack() + +class ChangeFrame(tk.Frame): + def __init__(self, root): + super().__init__(root) + self.name = tk.StringVar() + self.money = tk.StringVar() + self.status = tk.StringVar() + self.create_page() + + def create_page(self): + tk.Label(self).grid(row=0, pady=10) + + tk.Label(self, text='姓 名:').grid(row=1, column=1, pady=10) + tk.Entry(self, textvariable=self.name).grid(row=1, column=2, pady=10) + + tk.Label(self, text='金 额:').grid(row=2, column=1, pady=10) + tk.Entry(self, textvariable=self.money).grid(row=2, column=2, pady=10) + + tk.Button(self, text='查询', command=self.search_user).grid(row=3, column=1, pady=10) + tk.Button(self, text='修改', command=self.change_user).grid(row=3, column=2, pady=10) + + tk.Label(self, textvariable=self.status).grid(row=6, column=1, pady=10, stick=tk.E) + + def search_user(self): + flag, info = db.search_by_username(self.name.get()) + if flag: + self.name.set(info['name']) + self.money.set(info['money']) + self.status.set('数据查询成功') + else: + self.status.set(info) + + def change_user(self): + bk = {"name": self.name.get(), "money": self.money.get()} + self.name.set('') + self.money.set('') + print(bk) + db.update(bk) + self.status.set('修改数据成功') + +class InsertFrame(tk.Frame): + def __init__(self, root): + super().__init__(root) + # 动态跟踪变量值的变化 + self.name = tk.StringVar() + self.money = tk.StringVar() + self.status = tk.StringVar() + self.create_page() + + def create_page(self): + tk.Label(self).grid(row=0, pady=10) + + tk.Label(self, text='姓 名:').grid(row=1, column=1, pady=10) + tk.Entry(self, textvariable=self.name).grid(row=1, column=2, pady=10) + + tk.Label(self, text='金 额:').grid(row=2, column=1, pady=10) + tk.Entry(self, textvariable=self.money).grid(row=2, column=2, pady=10) + + tk.Button(self, text='录入', command=self.recode_info).grid(row=3, column=2, pady=10) + + tk.Label(self, textvariable=self.status).grid(row=6, column=1, pady=10, stick=tk.E) + + # 获取bkuser的信息 + def recode_info(self): + bk = {"name": self.name.get(), "money": self.money.get()} + self.name.set('') + self.money.set('') + print(bk) + db.insert(bk) + self.status.set('插入数据成功') + +class SearchFrame(tk.Frame): + def __init__(self, root): + super().__init__(root) + + self.table_view = tk.Frame() + self.table_view.pack() + + self.crete_page() + def crete_page(self): + columns = ("name", "money") + columns_values = ("姓名", "金额") + self.tree_view = ttk.Treeview(self, show='headings', columns=columns) + self.tree_view.column('name',width=160, anchor='center') + self.tree_view.column('money', width=160, anchor='center') + self.tree_view.heading('name', text='姓名') + self.tree_view.heading('money', text='金额') + self.tree_view.pack(fill=tk.BOTH, expand=True) + self.show_data_frame() + + tk.Button(self, text='刷新数据', command=self.show_data_frame).pack(anchor=tk.E, pady=5) + + def show_data_frame(self): + # 删除旧的节点 + for _ in map(self.tree_view.delete, self.tree_view.get_children('')): + pass + bkuser = db.all() + index = 0 + for bk in bkuser: + print(bk) + self.tree_view.insert('', index + 1, values=( + bk['name'], bk['money'], + )) + +class DeleteFrame(tk.Frame): + def __init__(self, root): + super().__init__(root) + self.username = tk.StringVar() + self.status = tk.StringVar() + tk.Label(self, text='根据名字删除数据').pack() + tk.Entry(self, textvariable=self.username).pack() + tk.Button(self, text='删除', command=self.delete).pack() + tk.Label(self, textvariable=self.status).pack() + + def delete(self): + username = self.username.get() + flag, message = db.delete_by_username(username) + self.status.set(message) \ No newline at end of file