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='关于软件:基于tkinter开发').pack() tk.Label(self, text='关于作者:黄晨轲').pack() tk.Label(self, text='开发时间:2024年5月').pack() class ChangeFrame(tk.Frame): def __init__(self,root): super().__init__(root) self.name = tk.StringVar() self.banji = tk.StringVar() self.xuehao = tk.StringVar() self.youbian = 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.banji).grid(row=2, column=2, pady=10) tk.Label(self, text='学 号:').grid(row=3, column=1, pady=10) tk.Entry(self, textvariable=self.xuehao).grid(row=3, column=2, pady=10) tk.Label(self, text='邮 编:').grid(row=4, column=1, pady=10) tk.Entry(self, textvariable=self.youbian).grid(row=4, column=2, pady=10) tk.Button(self, text='查询', command=self.search_user).grid(row=5, column=1, pady=10, stick=tk.E) tk.Button(self, text='修改',command=self.change_user).grid(row=5, column=2, pady=10,stick=tk.E) tk.Label(self, textvariable=self.status).grid(row=6, column=2, pady=10) def search_user(self): flag, info = db.search_by_username(self.name.get()) if flag: self.name.set(info['name']) self.banji.set(info['banji']) self.xuehao.set(info['xuehao']) self.youbian.set(info['youbian']) self.status.set('数据查询成功') else: self.status.set(info) def change_user(self): stu = {"name": self.name.get(),"banji": self.banji.get(), "xuehao": self.xuehao.get(), "youbian": self.youbian.get()} self.name.set('') self.banji.set('') self.xuehao.set('') self.youbian.set('') db.update(stu) self.status.set('修改数据成功') class InsertFrame(tk.Frame): def __init__(self,root): super().__init__(root) #tk.Label(self, text='录入页面').pack() self.name = tk.StringVar() self.banji = tk.StringVar() self.xuehao = tk.StringVar() self.youbian = 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.banji).grid(row=2, column=2, pady=10) tk.Label(self, text='学 号:').grid(row=3, column=1, pady=10) tk.Entry(self, textvariable=self.xuehao).grid(row=3, column=2, pady=10) tk.Label(self, text='邮 编:').grid(row=4, column=1, pady=10) tk.Entry(self, textvariable=self.youbian).grid(row=4, column=2, pady=10) tk.Button(self, text='录入',command=self.recode_info).grid(row=5, column=2, pady=10,stick=tk.E) tk.Label(self, textvariable=self.status).grid(row=6, column=2, pady=10) def recode_info(self): stu = {"name": self.name.get(),"banji": self.banji.get(), "xuehao": self.xuehao.get(), "youbian": self.youbian.get()} self.name.set('') self.banji.set('') self.xuehao.set('') self.youbian.set('') db.insert(stu) self.status.set('插入数据成功') 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): coulumns = ("name","xuehao","banji","youbian") coulumns_values = ("姓名","学号","班级","邮编") self.tree_view = ttk.Treeview(self, show='headings', columns=coulumns) self.tree_view.column('name',width=80,anchor='center') self.tree_view.column('xuehao', width=80, anchor='center') self.tree_view.column('banji', width=80, anchor='center') self.tree_view.column('youbian', width=80, anchor='center') self.tree_view.heading('name',text='姓名') self.tree_view.heading('xuehao', text='班级') self.tree_view.heading('banji', text='学号') self.tree_view.heading('youbian', 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 student = db.all() index = 0 for stu in student: print(stu) self.tree_view.insert('',index + 1,values=( stu['name'],stu['banji'],stu['xuehao'],stu['youbian'], )) 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)