帮我写一个有图形化界面的通讯录系统
时间: 2024-05-10 13:18:33 浏览: 99
好的,这个项目需要使用 GUI 框架和数据库来实现。我建议使用 Python 的 tkinter 模块作为 GUI 框架,以及 SQLite 作为数据库。
以下是实现步骤:
1. 首先安装 tkinter 和 SQLite3
```
pip install tkinter
pip install sqlite3
```
2. 创建一个 GUI 窗口,包含添加联系人、查找联系人、显示联系人等按钮。
```python
import tkinter as tk
root = tk.Tk()
root.title("通讯录系统")
# 添加按钮
add_button = tk.Button(root, text="添加联系人")
add_button.pack()
# 查找按钮
search_button = tk.Button(root, text="查找联系人")
search_button.pack()
# 显示按钮
show_button = tk.Button(root, text="显示联系人")
show_button.pack()
root.mainloop()
```
3. 创建一个数据库来存储联系人信息。在程序启动时,如果数据库不存在,则创建一个新的数据库。
```python
import sqlite3
conn = sqlite3.connect('contacts.db')
c = conn.cursor()
# 创建表格
c.execute('''CREATE TABLE IF NOT EXISTS contacts
(id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
phone TEXT,
email TEXT)''')
conn.commit()
conn.close()
```
4. 实现添加联系人功能。点击添加联系人按钮后,弹出一个窗口,要求输入联系人的姓名、电话和电子邮件,并将这些信息保存到数据库中。
```python
def add_contact():
# 创建新窗口
add_window = tk.Toplevel(root)
# 姓名标签和输入框
name_label = tk.Label(add_window, text="姓名")
name_label.pack()
name_entry = tk.Entry(add_window)
name_entry.pack()
# 电话标签和输入框
phone_label = tk.Label(add_window, text="电话")
phone_label.pack()
phone_entry = tk.Entry(add_window)
phone_entry.pack()
# 邮箱标签和输入框
email_label = tk.Label(add_window, text="邮箱")
email_label.pack()
email_entry = tk.Entry(add_window)
email_entry.pack()
# 添加按钮
def add():
name = name_entry.get()
phone = phone_entry.get()
email = email_entry.get()
conn = sqlite3.connect('contacts.db')
c = conn.cursor()
c.execute("INSERT INTO contacts (name, phone, email) VALUES (?, ?, ?)", (name, phone, email))
conn.commit()
conn.close()
add_window.destroy()
add_button = tk.Button(add_window, text="添加", command=add)
add_button.pack()
```
5. 实现查找联系人功能。点击查找联系人按钮后,弹出一个窗口,要求输入关键字(可以是姓名、电话或电子邮件),并显示匹配的联系人信息。
```python
def search_contact():
# 创建新窗口
search_window = tk.Toplevel(root)
# 关键字标签和输入框
keyword_label = tk.Label(search_window, text="关键字")
keyword_label.pack()
keyword_entry = tk.Entry(search_window)
keyword_entry.pack()
# 查找按钮
def search():
keyword = keyword_entry.get()
conn = sqlite3.connect('contacts.db')
c = conn.cursor()
c.execute("SELECT * FROM contacts WHERE name LIKE ? OR phone LIKE ? OR email LIKE ?", ('%' + keyword + '%', '%' + keyword + '%', '%' + keyword + '%'))
result = c.fetchall()
conn.close()
# 显示结果
for row in result:
tk.Label(search_window, text=row[1]).pack()
tk.Label(search_window, text=row[2]).pack()
tk.Label(search_window, text=row[3]).pack()
tk.Label(search_window, text="").pack()
search_button = tk.Button(search_window, text="查找", command=search)
search_button.pack()
```
6. 实现显示联系人功能。点击显示联系人按钮后,弹出一个窗口,显示所有联系人的信息。
```python
def show_contacts():
# 创建新窗口
show_window = tk.Toplevel(root)
conn = sqlite3.connect('contacts.db')
c = conn.cursor()
c.execute("SELECT * FROM contacts")
result = c.fetchall()
conn.close()
# 显示结果
for row in result:
tk.Label(show_window, text=row[1]).pack()
tk.Label(show_window, text=row[2]).pack()
tk.Label(show_window, text=row[3]).pack()
tk.Label(show_window, text="").pack()
show_button = tk.Button(root, text="显示联系人", command=show_contacts)
show_button.pack()
```
完整代码如下:
```python
import tkinter as tk
import sqlite3
# 创建数据库
conn = sqlite3.connect('contacts.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS contacts
(id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
phone TEXT,
email TEXT)''')
conn.commit()
conn.close()
# 添加联系人
def add_contact():
# 创建新窗口
add_window = tk.Toplevel(root)
# 姓名标签和输入框
name_label = tk.Label(add_window, text="姓名")
name_label.pack()
name_entry = tk.Entry(add_window)
name_entry.pack()
# 电话标签和输入框
phone_label = tk.Label(add_window, text="电话")
phone_label.pack()
phone_entry = tk.Entry(add_window)
phone_entry.pack()
# 邮箱标签和输入框
email_label = tk.Label(add_window, text="邮箱")
email_label.pack()
email_entry = tk.Entry(add_window)
email_entry.pack()
# 添加按钮
def add():
name = name_entry.get()
phone = phone_entry.get()
email = email_entry.get()
conn = sqlite3.connect('contacts.db')
c = conn.cursor()
c.execute("INSERT INTO contacts (name, phone, email) VALUES (?, ?, ?)", (name, phone, email))
conn.commit()
conn.close()
add_window.destroy()
add_button = tk.Button(add_window, text="添加", command=add)
add_button.pack()
# 查找联系人
def search_contact():
# 创建新窗口
search_window = tk.Toplevel(root)
# 关键字标签和输入框
keyword_label = tk.Label(search_window, text="关键字")
keyword_label.pack()
keyword_entry = tk.Entry(search_window)
keyword_entry.pack()
# 查找按钮
def search():
keyword = keyword_entry.get()
conn = sqlite3.connect('contacts.db')
c = conn.cursor()
c.execute("SELECT * FROM contacts WHERE name LIKE ? OR phone LIKE ? OR email LIKE ?", ('%' + keyword + '%', '%' + keyword + '%', '%' + keyword + '%'))
result = c.fetchall()
conn.close()
# 显示结果
for row in result:
tk.Label(search_window, text=row[1]).pack()
tk.Label(search_window, text=row[2]).pack()
tk.Label(search_window, text=row[3]).pack()
tk.Label(search_window, text="").pack()
search_button = tk.Button(search_window, text="查找", command=search)
search_button.pack()
# 显示联系人
def show_contacts():
# 创建新窗口
show_window = tk.Toplevel(root)
conn = sqlite3.connect('contacts.db')
c = conn.cursor()
c.execute("SELECT * FROM contacts")
result = c.fetchall()
conn.close()
# 显示结果
for row in result:
tk.Label(show_window, text=row[1]).pack()
tk.Label(show_window, text=row[2]).pack()
tk.Label(show_window, text=row[3]).pack()
tk.Label(show_window, text="").pack()
# 创建 GUI 窗口
root = tk.Tk()
root.title("通讯录系统")
# 添加按钮
add_button = tk.Button(root, text="添加联系人", command=add_contact)
add_button.pack()
# 查找按钮
search_button = tk.Button(root, text="查找联系人", command=search_contact)
search_button.pack()
# 显示按钮
show_button = tk.Button(root, text="显示联系人", command=show_contacts)
show_button.pack()
root.mainloop()
```
阅读全文