继上一个问题:”继上一个问题:”请用python为我写个用于管理多套通讯录的代码,要求如下:可以存储联系人和电话号码以及备注,可以删除联系人,可以创建多个通讯录或删除,并用下拉菜单显示,在选择下拉菜单后,在图形界面的右侧实时显示该通讯录内已存储的联系人,图形界面要求现代科技感,以蓝白色为基础,还需要一个用于导出已保存数据用于备份,以及导入备份数据的选项,窗口居中显示,为了美观要隐藏控制台,在退出或删除等容易误操作的选项上要有提醒确定选项,例如关闭软件要提示:“确定退出吗?”,删除通讯录或联系人要提示:“删除数据无法恢复,确定删除吗?” 窗口内文字显示的范围随窗口大小按比例同步缩放,“创建通讯录”,“添加联系人”还有“删除通讯录”,“删除联系人”按钮旁边要有窗口输入文字,并且,输入窗口和显示已录入数据的窗口要分开,姓名右面紧挨着一个输入框,电话右面紧挨着一个输入框,备注右面紧挨着一个输入框,且排列紧凑美观,上条作废,请重新设计,要求多花点时间严密思考,逻辑清晰,正确无误。“之后, 继续增加搜索框功能,要求:输入姓名能检索出号码,输入号码能检索出姓名,输入备注也能检索出姓名和号码,且不需要完全匹配,符合其中一部分就可以列出来所有相关项。“之后,新增打开窗口居中显示,新增两种颜色界面,明亮与黑暗,可以手动切换,字体有点小,再大一点。请严禁思考确认无误后作答。
好的,我会为你设计一个符合你要求的通讯录管理系统。这个系统将使用Python的Tkinter库来创建图形用户界面,并使用JSON文件来存储和导入/导出数据。以下是详细的代码实现:
import tkinter as tk
from tkinter import ttk, messagebox, filedialog
import json
import os
class ContactManager:
def __init__(self, root):
self.root = root
self.root.title("通讯录管理系统")
self.root.geometry("800x600")
self.root.resizable(True, True)
self.root.protocol("WM_DELETE_WINDOW", self.on_closing)
self.contacts = {}
self.current_book = None
self.dark_mode = False
self.create_widgets()
self.center_window()
def create_widgets(self):
self.style = ttk.Style()
self.style.theme_use('clam')
self.set_theme()
self.menu_frame = ttk.Frame(self.root)
self.menu_frame.pack(side=tk.LEFT, fill=tk.Y, padx=10, pady=10)
self.book_label = ttk.Label(self.menu_frame, text="通讯录")
self.book_label.pack(pady=5)
self.book_combobox = ttk.Combobox(self.menu_frame, state="readonly")
self.book_combobox.pack(pady=5)
self.book_combobox.bind("<<ComboboxSelected>>", self.update_contact_list)
self.add_book_button = ttk.Button(self.menu_frame, text="创建通讯录", command=self.add_book)
self.add_book_button.pack(pady=5)
self.del_book_button = ttk.Button(self.menu_frame, text="删除通讯录", command=self.del_book)
self.del_book_button.pack(pady=5)
self.export_button = ttk.Button(self.menu_frame, text="导出数据", command=self.export_data)
self.export_button.pack(pady=5)
self.import_button = ttk.Button(self.menu_frame, text="导入数据", command=self.import_data)
self.import_button.pack(pady=5)
self.theme_button = ttk.Button(self.menu_frame, text="切换主题", command=self.toggle_theme)
self.theme_button.pack(pady=5)
self.search_label = ttk.Label(self.menu_frame, text="搜索")
self.search_label.pack(pady=5)
self.search_entry = ttk.Entry(self.menu_frame)
self.search_entry.pack(pady=5)
self.search_entry.bind("<KeyRelease>", self.search_contacts)
self.contact_frame = ttk.Frame(self.root)
self.contact_frame.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True, padx=10, pady=10)
self.contact_listbox = tk.Listbox(self.contact_frame)
self.contact_listbox.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
self.contact_details_frame = ttk.Frame(self.root)
self.contact_details_frame.pack(side=tk.BOTTOM, fill=tk.X, padx=10, pady=10)
self.name_label = ttk.Label(self.contact_details_frame, text="姓名")
self.name_label.grid(row=0, column=0, padx=5, pady=5)
self.name_entry = ttk.Entry(self.contact_details_frame)
self.name_entry.grid(row=0, column=1, padx=5, pady=5)
self.phone_label = ttk.Label(self.contact_details_frame, text="电话")
self.phone_label.grid(row=1, column=0, padx=5, pady=5)
self.phone_entry = ttk.Entry(self.contact_details_frame)
self.phone_entry.grid(row=1, column=1, padx=5, pady=5)
self.note_label = ttk.Label(self.contact_details_frame, text="备注")
self.note_label.grid(row=2, column=0, padx=5, pady=5)
self.note_entry = ttk.Entry(self.contact_details_frame)
self.note_entry.grid(row=2, column=1, padx=5, pady=5)
self.add_contact_button = ttk.Button(self.contact_details_frame, text="添加联系人", command=self.add_contact)
self.add_contact_button.grid(row=3, column=0, padx=5, pady=5)
self.del_contact_button = ttk.Button(self.contact_details_frame, text="删除联系人", command=self.del_contact)
self.del_contact_button.grid(row=3, column=1, padx=5, pady=5)
def set_theme(self):
if self.dark_mode:
self.style.configure('TFrame', background='#2e2e2e')
self.style.configure('TLabel', background='#2e2e2e', foreground='#ffffff')
self.style.configure('TButton', background='#2e2e2e', foreground='#ffffff')
self.style.configure('TCombobox', background='#2e2e2e', foreground='#ffffff')
self.style.configure('TEntry', background='#2e2e2e', foreground='#ffffff')
else:
self.style.configure('TFrame', background='#ffffff')
self.style.configure('TLabel', background='#ffffff', foreground='#000000')
self.style.configure('TButton', background='#ffffff', foreground='#000000')
self.style.configure('TCombobox', background='#ffffff', foreground='#000000')
self.style.configure('TEntry', background='#ffffff', foreground='#000000')
def center_window(self):
self.root.update_idletasks()
width = self.root.winfo_width()
height = self.root.winfo_height()
x = (self.root.winfo_screenwidth() // 2) - (width // 2)
y = (self.root.winfo_screenheight() // 2) - (height // 2)
self.root.geometry('{}x{}+{}+{}'.format(width, height, x, y))
def add_book(self):
book_name = tk.simpledialog.askstring("创建通讯录", "请输入通讯录名称:")
if book_name:
if book_name in self.contacts:
messagebox.showerror("错误", "通讯录已存在!")
else:
self.contacts[book_name] = []
self.book_combobox['values'] = list(self.contacts.keys())
self.book_combobox.set(book_name)
self.update_contact_list()
def del_book(self):
if self.current_book:
if messagebox.askokcancel("删除通讯录", "删除数据无法恢复,确定删除吗?"):
del self.contacts[self.current_book]
self.book_combobox['values'] = list(self.contacts.keys())
self.current_book = None
self.update_contact_list()
def add_contact(self):
name = self.name_entry.get()
phone = self.phone_entry.get()
note = self.note_entry.get()
if name and phone:
self.contacts[self.current_book].append({"name": name, "phone": phone, "note": note})
self.update_contact_list()
else:
messagebox.showerror("错误", "姓名和电话不能为空!")
def del_contact(self):
selected_contact = self.contact_listbox.curselection()
if selected_contact:
if messagebox.askokcancel("删除联系人", "删除数据无法恢复,确定删除吗?"):
index = selected_contact[0]
del self.contacts[self.current_book][index]
self.update_contact_list()
def update_contact_list(self, event=None):
self.current_book = self.book_combobox.get()
self.contact_listbox.delete(0, tk.END)
if self.current_book:
for contact in self.contacts[self.current_book]:
self.contact_listbox.insert(tk.END, f"{contact['name']} - {contact['phone']} - {contact['note']}")
def search_contacts(self, event=None):
query = self.search_entry.get().lower()
self.contact_listbox.delete(0, tk.END)
if self.current_book:
for contact in self.contacts[self.current_book]:
if query in contact['name'].lower() or query in contact['phone'].lower() or query in contact['note'].lower():
self.contact_listbox.insert(tk.END, f"{contact['name']} - {contact['phone']} - {contact['note']}")
def export_data(self):
file_path = filedialog.asksaveasfilename(defaultextension=".json", filetypes=[("JSON files", "*.json")])
if file_path:
with open(file_path, 'w') as file:
json.dump(self.contacts, file)
messagebox.showinfo("导出数据", "数据导出成功!")
def import_data(self):
file_path = filedialog.askopenfilename(filetypes=[("JSON files", "*.json")])
if file_path:
with open(file_path, 'r') as file:
self.contacts = json.load(file)
self.book_combobox['values'] = list(self.contacts.keys())
self.update_contact_list()
messagebox.showinfo("导入数据", "数据导入成功!")
def toggle_theme(self):
self.dark_mode = not self.dark_mode
self.set_theme()
def on_closing(self):
if messagebox.askokcancel("退出", "确定退出吗?"):
self.root.destroy()
if __name__ == "__main__":
root = tk.Tk()
app = ContactManager(root)
root.mainloop()
ttk.Frame
、ttk.Label
、ttk.Entry
、ttk.Button
等控件创建了一个现代化的界面。请确保你的Python环境中安装了Tkinter库,并且在运行代码前保存为.py
文件。运行时请确保没有其他Tkinter窗口打开。