使用tkinter实现简单的通信录功能
时间: 2023-04-22 07:00:35 浏览: 93
使用tkinter实现简单的通信录功能可以分为以下几个步骤:
1. 导入tkinter模块和其他需要的模块,如sqlite3模块用于数据库操作。
2. 创建主窗口,并设置窗口标题、大小等属性。
3. 创建通信录数据库表,包括姓名、电话、邮箱等字段。
4. 创建通信录添加、删除、修改、查询等功能的函数,并在主窗口中添加对应的按钮和文本框等控件。
5. 运行主窗口,等待用户操作。
下面是一个简单的示例代码:
```python
import tkinter as tk
import sqlite3
# 创建主窗口
root = tk.Tk()
root.title('通信录')
root.geometry('400x300')
# 创建数据库连接
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()
# 添加联系人
def add_contact():
name = name_entry.get()
phone = phone_entry.get()
email = email_entry.get()
c.execute("INSERT INTO contacts (name, phone, email) VALUES (?, ?, ?)", (name, phone, email))
conn.commit()
name_entry.delete(0, tk.END)
phone_entry.delete(0, tk.END)
email_entry.delete(0, tk.END)
# 删除联系人
def delete_contact():
name = name_entry.get()
c.execute("DELETE FROM contacts WHERE name=?", (name,))
conn.commit()
name_entry.delete(0, tk.END)
phone_entry.delete(0, tk.END)
email_entry.delete(0, tk.END)
# 修改联系人
def update_contact():
name = name_entry.get()
phone = phone_entry.get()
email = email_entry.get()
c.execute("UPDATE contacts SET phone=?, email=? WHERE name=?", (phone, email, name))
conn.commit()
name_entry.delete(0, tk.END)
phone_entry.delete(0, tk.END)
email_entry.delete(0, tk.END)
# 查询联系人
def search_contact():
name = name_entry.get()
c.execute("SELECT * FROM contacts WHERE name=?", (name,))
result = c.fetchone()
if result:
phone_entry.delete(0, tk.END)
email_entry.delete(0, tk.END)
phone_entry.insert(0, result[2])
email_entry.insert(0, result[3])
# 创建控件
name_label = tk.Label(root, text='姓名:')
name_label.grid(row=0, column=0)
name_entry = tk.Entry(root)
name_entry.grid(row=0, column=1)
phone_label = tk.Label(root, text='电话:')
phone_label.grid(row=1, column=0)
phone_entry = tk.Entry(root)
phone_entry.grid(row=1, column=1)
email_label = tk.Label(root, text='邮箱:')
email_label.grid(row=2, column=0)
email_entry = tk.Entry(root)
email_entry.grid(row=2, column=1)
add_button = tk.Button(root, text='添加', command=add_contact)
add_button.grid(row=3, column=0)
delete_button = tk.Button(root, text='删除', command=delete_contact)
delete_button.grid(row=3, column=1)
update_button = tk.Button(root, text='修改', command=update_contact)
update_button.grid(row=4, column=0)
search_button = tk.Button(root, text='查询', command=search_contact)
search_button.grid(row=4, column=1)
# 运行主窗口
root.mainloop()
# 关闭数据库连接
conn.close()
```
在这个示例中,我们使用了sqlite3模块来创建通信录数据库表,并实现了添加、删除、修改、查询等功能。在主窗口中,我们使用了Label、Entry、Button等控件来显示和获取用户输入的信息。当用户点击添加、删除、修改、查询按钮时,对应的函数会被调用,从而实现相应的功能。最后,我们使用mainloop()方法来运行主窗口,等待用户操作。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)