手机通讯录python代码
时间: 2023-11-16 12:01:35 浏览: 172
Python通讯录程序代码
5星 · 资源好评率100%
以下是一个简单的手机通讯录Python代码,它包括添加联系人、查找联系人、修改联系人和删除联系人等功能。这个程序使用了GUI图形界面来实现通讯录功能。
```python
# -*- coding: utf-8 -*-
import tkinter as tk
class Contact:
def __init__(self, name, phone, email):
self.name = name
self.phone = phone
self.email = email
class ContactBook:
def __init__(self):
self.contacts = []
def add_contact(self, contact):
self.contacts.append(contact)
def find_contact(self, name):
for contact in self.contacts:
if contact.name == name:
return contact
return None
def update_contact(self, name, phone, email):
contact = self.find_contact(name)
if contact:
contact.phone = phone
contact.email = email
def remove_contact(self, name):
contact = self.find_contact(name)
if contact:
self.contacts.remove(contact)
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.master.title("手机通讯录")
self.pack()
self.create_widgets()
def create_widgets(self):
self.name_label = tk.Label(self, text="姓名")
self.name_label.grid(row=0, column=0)
self.name_entry = tk.Entry(self)
self.name_entry.grid(row=0, column=1)
self.phone_label = tk.Label(self, text="电话")
self.phone_label.grid(row=1, column=0)
self.phone_entry = tk.Entry(self)
self.phone_entry.grid(row=1, column=1)
self.email_label = tk.Label(self, text="邮箱")
self.email_label.grid(row=2, column=0)
self.email_entry = tk.Entry(self)
self.email_entry.grid(row=2, column=1)
self.add_button = tk.Button(self, text="添加", command=self.add_contact)
self.add_button.grid(row=3, column=0)
self.find_button = tk.Button(self, text="查找", command=self.find_contact)
self.find_button.grid(row=3, column=1)
self.update_button = tk.Button(self, text="修改", command=self.update_contact)
self.update_button.grid(row=3, column=2)
self.remove_button = tk.Button(self, text="删除", command=self.remove_contact)
self.remove_button.grid(row=3, column=3)
self.result_label = tk.Label(self, text="")
self.result_label.grid(row=4, column=0, columnspan=4)
def add_contact(self):
name = self.name_entry.get()
phone = self.phone_entry.get()
email = self.email_entry.get()
contact = Contact(name, phone, email)
contact_book.add_contact(contact)
self.result_label.config(text="添加联系人成功")
def find_contact(self):
name = self.name_entry.get()
contact = contact_book.find_contact(name)
if contact:
self.phone_entry.delete(0, tk.END)
self.phone_entry.insert(0, contact.phone)
self.email_entry.delete(0, tk.END)
self.email_entry.insert(0, contact.email)
self.result_label.config(text="")
else:
self.result_label.config(text="联系人不存在")
def update_contact(self):
name = self.name_entry.get()
phone = self.phone_entry.get()
email = self.email_entry.get()
contact_book.update_contact(name, phone, email)
self.result_label.config(text="修改联系人成功")
def remove_contact(self):
name = self.name_entry.get()
contact_book.remove_contact(name)
self.result_label.config(text="删除联系人成功")
contact_book = ContactBook()
root = tk.Tk()
app = Application(master=root)
app.mainloop()
```
阅读全文