python设计通讯录实现增删改查功能并录入excel文档中
时间: 2024-03-11 16:51:40 浏览: 87
可以使用Python中的xlwt和xlrd模块来读、写Excel文件,同时使用tkinter模块来设计通讯录系统的GUI界面。下面是一个示例代码,用于设计通讯录系统并实现增删改查功能,并将数据录入Excel文档中:
```
import tkinter as tk
import xlwt
import xlrd
class AddressBook:
def __init__(self, master):
self.master = master
master.title("通讯录系统")
# 创建GUI界面
self.label_name = tk.Label(master, text="姓名:")
self.label_name.grid(row=0, column=0)
self.entry_name = tk.Entry(master)
self.entry_name.grid(row=0, column=1)
self.label_phone = tk.Label(master, text="电话:")
self.label_phone.grid(row=1, column=0)
self.entry_phone = tk.Entry(master)
self.entry_phone.grid(row=1, column=1)
self.label_email = tk.Label(master, text="邮箱:")
self.label_email.grid(row=2, column=0)
self.entry_email = tk.Entry(master)
self.entry_email.grid(row=2, column=1)
# 添加按钮
self.button_add = tk.Button(master, text="添加", command=self.add_contact)
self.button_add.grid(row=3, column=0)
# 删除按钮
self.button_delete = tk.Button(master, text="删除", command=self.delete_contact)
self.button_delete.grid(row=3, column=1)
# 修改按钮
self.button_edit = tk.Button(master, text="修改", command=self.edit_contact)
self.button_edit.grid(row=3, column=2)
# 查询按钮
self.button_search = tk.Button(master, text="查询", command=self.search_contact)
self.button_search.grid(row=3, column=3)
# 创建Excel文件和Sheet
self.workbook = xlwt.Workbook()
self.worksheet = self.workbook.add_sheet("通讯录")
self.row = 0
self.col = 0
# 写入表头
self.worksheet.write(self.row, self.col, "姓名")
self.col += 1
self.worksheet.write(self.row, self.col, "电话")
self.col += 1
self.worksheet.write(self.row, self.col, "邮箱")
self.row += 1
self.col = 0
def add_contact(self):
# 获取用户输入的信息
name = self.entry_name.get()
phone = self.entry_phone.get()
email = self.entry_email.get()
# 写入数据到Excel文件中
self.worksheet.write(self.row, self.col, name)
self.col += 1
self.worksheet.write(self.row, self.col, phone)
self.col += 1
self.worksheet.write(self.row, self.col, email)
self.row += 1
self.col = 0
# 清空文本框
self.entry_name.delete(0, tk.END)
self.entry_phone.delete(0, tk.END)
self.entry_email.delete(0, tk.END)
# 保存到文件
self.workbook.save("通讯录.xls")
def delete_contact(self):
# 获取用户输入的姓名
name = self.entry_name.get()
# 遍历Excel文件,查找要删除的行
for i in range(1, self.row):
if self.worksheet.cell_value(i, 0) == name:
# 删除行
self.worksheet._Worksheet__rows.pop(i)
self.row -= 1
# 保存到文件
self.workbook.save("通讯录.xls")
break
# 清空文本框
self.entry_name.delete(0, tk.END)
self.entry_phone.delete(0, tk.END)
self.entry_email.delete(0, tk.END)
def edit_contact(self):
# 获取用户输入的姓名
name = self.entry_name.get()
# 遍历Excel文件,查找要编辑的行
for i in range(1, self.row):
if self.worksheet.cell_value(i, 0) == name:
# 获取用户输入的信息
phone = self.entry_phone.get()
email = self.entry_email.get()
# 修改数据
self.worksheet.write(i, 1, phone)
self.worksheet.write(i, 2, email)
# 保存到文件
self.workbook.save("通讯录.xls")
break
# 清空文本框
self.entry_name.delete(0, tk.END)
self.entry_phone.delete(0, tk.END)
self.entry_email.delete(0, tk.END)
def search_contact(self):
# 获取用户输入的姓名
name = self.entry_name.get()
# 遍历Excel文件,查找要查询的行
for i in range(1, self.row):
if self.worksheet.cell_value(i, 0) == name:
# 显示查询结果
self.entry_phone.delete(0, tk.END)
self.entry_phone.insert(0, self.worksheet.cell_value(i, 1))
self.entry_email.delete(0, tk.END)
self.entry_email.insert(0, self.worksheet.cell_value(i, 2))
break
def load_contacts(self):
# 加载Excel文件中的数据
workbook = xlrd.open_workbook("通讯录.xls")
worksheet = workbook.sheet_by_index(0)
self.row = worksheet.nrows
# 遍历Excel文件中的数据,并显示到GUI界面中
for i in range(1, self.row):
name = worksheet.cell_value(i, 0)
phone = worksheet.cell_value(i, 1)
email = worksheet.cell_value(i, 2)
self.worksheet.write(self.row, self.col, name)
self.col += 1
self.worksheet.write(self.row, self.col, phone)
self.col += 1
self.worksheet.write(self.row, self.col, email)
self.row += 1
self.col = 0
root = tk.Tk()
address_book = AddressBook(root)
address_book.load_contacts()
root.mainloop()
```
在上面的示例代码中,我们使用tkinter模块创建了一个GUI界面,包含文本框和四个按钮,分别对应添加、删除、修改和查询操作。当用户点击这些按钮时,我们会从Excel文件中读取、修改、删除或查询数据,并将结果显示到GUI界面中。
同时,我们也在GUI界面中显示了已经录入的数据,并在程序启动时从Excel文件中读取数据。最后,我们将Excel文件保存到了本地磁盘上。
阅读全文