界面能实现数据的增加,修改,删除,查询操作并实现类实现数据封装,能使文件保存数据
时间: 2024-03-12 22:46:42 浏览: 155
好的,以下是一个简单的使用tkinter实现增删改查并保存数据的代码示例:
```python
import tkinter as tk
import tkinter.messagebox as messagebox
import pickle
class Person:
def __init__(self, name='', age=0, gender=''):
self.name = name
self.age = age
self.gender = gender
class App:
def __init__(self, master):
self.master = master
self.master.title('人员信息管理系统')
self.master.geometry('600x400')
# 初始化数据
self.data = []
self.load_data()
# 创建界面
self.create_widgets()
def load_data(self):
try:
with open('data.dat', 'rb') as f:
self.data = pickle.load(f)
except:
pass
def save_data(self):
with open('data.dat', 'wb') as f:
pickle.dump(self.data, f)
def create_widgets(self):
# 创建菜单栏
menubar = tk.Menu(self.master)
filemenu = tk.Menu(menubar, tearoff=0)
filemenu.add_command(label='保存', command=self.save_data)
filemenu.add_separator()
filemenu.add_command(label='退出', command=self.master.quit)
menubar.add_cascade(label='文件', menu=filemenu)
self.master.config(menu=menubar)
# 创建列表框和滚动条
self.listbox = tk.Listbox(self.master, width=60, height=20)
self.listbox.grid(row=0, column=0, padx=10, pady=10)
scrollbar = tk.Scrollbar(self.master)
scrollbar.grid(row=0, column=1, sticky=tk.NS, padx=0, pady=10)
self.listbox.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=self.listbox.yview)
# 创建按钮
button_frame = tk.Frame(self.master)
button_frame.grid(row=1, column=0, padx=10, pady=10)
add_button = tk.Button(button_frame, text='添加', command=self.add_person)
add_button.grid(row=0, column=0, padx=10, pady=10)
modify_button = tk.Button(button_frame, text='修改', command=self.modify_person)
modify_button.grid(row=0, column=1, padx=10, pady=10)
delete_button = tk.Button(button_frame, text='删除', command=self.delete_person)
delete_button.grid(row=0, column=2, padx=10, pady=10)
search_button = tk.Button(button_frame, text='查询', command=self.search_person)
search_button.grid(row=0, column=3, padx=10, pady=10)
def add_person(self):
# 创建添加窗口
add_window = tk.Toplevel(self.master)
add_window.title('添加人员信息')
add_window.geometry('300x200')
# 创建输入框
name_label = tk.Label(add_window, text='姓名:')
name_label.grid(row=0, column=0, padx=10, pady=20)
name_entry = tk.Entry(add_window)
name_entry.grid(row=0, column=1, padx=10, pady=20)
age_label = tk.Label(add_window, text='年龄:')
age_label.grid(row=1, column=0, padx=10, pady=20)
age_entry = tk.Entry(add_window)
age_entry.grid(row=1, column=1, padx=10, pady=20)
gender_label = tk.Label(add_window, text='性别:')
gender_label.grid(row=2, column=0, padx=10, pady=20)
gender_entry = tk.Entry(add_window)
gender_entry.grid(row=2, column=1, padx=10, pady=20)
# 创建确定按钮
ok_button = tk.Button(add_window, text='确定', command=lambda: self.add_person_callback(add_window, name_entry, age_entry, gender_entry))
ok_button.grid(row=3, column=0, columnspan=2, padx=10, pady=20)
def add_person_callback(self, add_window, name_entry, age_entry, gender_entry):
name = name_entry.get()
age = age_entry.get()
gender = gender_entry.get()
if name and age and gender:
person = Person(name, age, gender)
self.data.append(person)
self.update_listbox()
add_window.destroy()
else:
messagebox.showerror('错误', '请填写完整信息')
def modify_person(self):
# 获取选中的人员信息
index = self.listbox.curselection()
if not index:
messagebox.showerror('错误', '请选择要修改的人员信息')
return
person = self.data[index[0]]
# 创建修改窗口
modify_window = tk.Toplevel(self.master)
modify_window.title('修改人员信息')
modify_window.geometry('300x200')
# 创建输入框
name_label = tk.Label(modify_window, text='姓名:')
name_label.grid(row=0, column=0, padx=10, pady=20)
name_entry = tk.Entry(modify_window)
name_entry.grid(row=0, column=1, padx=10, pady=20)
name_entry.insert(0, person.name)
age_label = tk.Label(modify_window, text='年龄:')
age_label.grid(row=1, column=0, padx=10, pady=20)
age_entry = tk.Entry(modify_window)
age_entry.grid(row=1, column=1, padx=10, pady=20)
age_entry.insert(0, person.age)
gender_label = tk.Label(modify_window, text='性别:')
gender_label.grid(row=2, column=0, padx=10, pady=20)
gender_entry = tk.Entry(modify_window)
gender_entry.grid(row=2, column=1, padx=10, pady=20)
gender_entry.insert(0, person.gender)
# 创建确定按钮
ok_button = tk.Button(modify_window, text='确定', command=lambda: self.modify_person_callback(modify_window, person, name_entry, age_entry, gender_entry))
ok_button.grid(row=3, column=0, columnspan=2, padx=10, pady=20)
def modify_person_callback(self, modify_window, person, name_entry, age_entry, gender_entry):
name = name_entry.get()
age = age_entry.get()
gender = gender_entry.get()
if name and age and gender:
person.name = name
person.age = age
person.gender = gender
self.update_listbox()
modify_window.destroy()
else:
messagebox.showerror('错误', '请填写完整信息')
def delete_person(self):
# 获取选中的人员信息
index = self.listbox.curselection()
if not index:
messagebox.showerror('错误', '请选择要删除的人员信息')
return
person = self.data[index[0]]
# 确认删除
if messagebox.askyesno('确认', '确认删除该人员信息吗?'):
self.data.remove(person)
self.update_listbox()
def search_person(self):
# 创建查询窗口
search_window = tk.Toplevel(self.master)
search_window.title('查询人员信息')
search_window.geometry('300x200')
# 创建输入框
name_label = tk.Label(search_window, text='姓名:')
name_label.grid(row=0, column=0, padx=10, pady=20)
name_entry = tk.Entry(search_window)
name_entry.grid(row=0, column=1, padx=10, pady=20)
age_label = tk.Label(search_window, text='年龄:')
age_label.grid(row=1, column=0, padx=10, pady=20)
age_entry = tk.Entry(search_window)
age_entry.grid(row=1, column=1, padx=10, pady=20)
gender_label = tk.Label(search_window, text='性别:')
gender_label.grid(row=2, column=0, padx=10, pady=20)
gender_entry = tk.Entry(search_window)
gender_entry.grid(row=2, column=1, padx=10, pady=20)
# 创建确定按钮
ok_button = tk.Button(search_window, text='确定', command=lambda: self.search_person_callback(search_window, name_entry.get(), age_entry.get(), gender_entry.get()))
ok_button.grid(row=3, column=0, columnspan=2, padx=10, pady=20)
def search_person_callback(self, search_window, name, age, gender):
result = []
for person in self.data:
if (not name or person.name == name) and (not age or person.age == age) and (not gender or person.gender == gender):
result.append(person)
if result:
messagebox.showinfo('查询结果', '\n'.join([f'姓名:{person.name},年龄:{person.age},性别:{person.gender}' for person in result]))
else:
messagebox.showinfo('查询结果', '没有符合条件的人员信息')
search_window.destroy()
def update_listbox(self):
self.listbox.delete(0, tk.END)
for person in self.data:
self.listbox.insert(tk.END, f'姓名:{person.name},年龄:{person.age},性别:{person.gender}')
if __name__ == '__main__':
root = tk.Tk()
app = App(root)
root.mainloop()
```
在这个代码示例中,我们创建了一个简单的人员信息管理系统,可以实现数据的增加、修改、删除、查询,并且能够将数据保存到文件中。我们使用了面向对象的编程方式,将所有的人员信息封装到了一个 Person 类中。在界面方面,我们使用了 tk.Listbox 和 tk.Entry 来显示和输入人员信息,使用 tk.Button 来触发各种操作。我们还使用了 tk.Menu 创建了一个菜单栏,用来保存数据并退出程序。在数据存储方面,我们使用了 pickle 库将数据保存到了一个二进制文件中。
阅读全文