python班级通讯录代码
时间: 2023-07-09 09:42:16 浏览: 110
以下是一个Python班级通讯录项目的示例代码,可以作为参考:
```python
import sqlite3
import tkinter as tk
from tkinter import ttk, messagebox
class Student:
def __init__(self, id, name, phone, email):
self.id = id
self.name = name
self.phone = phone
self.email = email
class AddressBook:
def __init__(self):
self.conn = sqlite3.connect("address_book.db")
self.cursor = self.conn.cursor()
self.create_table()
def create_table(self):
self.cursor.execute("""CREATE TABLE IF NOT EXISTS students (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
phone TEXT,
email TEXT
)""")
self.conn.commit()
def add_student(self, name, phone, email):
self.cursor.execute("INSERT INTO students (name, phone, email) VALUES (?, ?, ?)", (name, phone, email))
self.conn.commit()
def delete_student(self, id):
self.cursor.execute("DELETE FROM students WHERE id=?", (id,))
self.conn.commit()
def update_student(self, id, name, phone, email):
self.cursor.execute("UPDATE students SET name=?, phone=?, email=? WHERE id=?", (name, phone, email, id))
self.conn.commit()
def search_student(self, search_term):
self.cursor.execute("SELECT * FROM students WHERE name LIKE ? OR phone LIKE ? OR email LIKE ?", ('%' + search_term + '%', '%' + search_term + '%', '%' + search_term + '%'))
results = self.cursor.fetchall()
students = []
for row in results:
student = Student(row[0], row[1], row[2], row[3])
students.append(student)
return students
def get_all_students(self):
self.cursor.execute("SELECT * FROM students")
results = self.cursor.fetchall()
students = []
for row in results:
student = Student(row[0], row[1], row[2], row[3])
students.append(student)
return students
class App:
def __init__(self):
self.address_book = AddressBook()
self.create_gui()
def create_gui(self):
self.root = tk.Tk()
self.root.title("班级通讯录")
self.name_label = ttk.Label(self.root, text="姓名:")
self.name_label.grid(row=0, column=0)
self.name_entry = ttk.Entry(self.root)
self.name_entry.grid(row=0, column=1)
self.phone_label = ttk.Label(self.root, text="电话:")
self.phone_label.grid(row=1, column=0)
self.phone_entry = ttk.Entry(self.root)
self.phone_entry.grid(row=1, column=1)
self.email_label = ttk.Label(self.root, text="邮箱:")
self.email_label.grid(row=2, column=0)
self.email_entry = ttk.Entry(self.root)
self.email_entry.grid(row=2, column=1)
self.add_button = ttk.Button(self.root, text="添加", command=self.add_student)
self.add_button.grid(row=3, column=0)
self.update_button = ttk.Button(self.root, text="修改", command=self.update_student)
self.update_button.grid(row=3, column=1)
self.delete_button = ttk.Button(self.root, text="删除", command=self.delete_student)
self.delete_button.grid(row=3, column=2)
self.search_label = ttk.Label(self.root, text="搜索:")
self.search_label.grid(row=4, column=0)
self.search_entry = ttk.Entry(self.root)
self.search_entry.grid(row=4, column=1)
self.search_button = ttk.Button(self.root, text="搜索", command=self.search_student)
self.search_button.grid(row=4, column=2)
self.student_listbox = tk.Listbox(self.root, width=50, height=10)
self.student_listbox.grid(row=5, column=0, columnspan=3)
self.load_students()
def add_student(self):
name = self.name_entry.get()
phone = self.phone_entry.get()
email = self.email_entry.get()
self.address_book.add_student(name, phone, email)
self.load_students()
def update_student(self):
selected_index = self.student_listbox.curselection()
if len(selected_index) == 0:
messagebox.showwarning("错误", "请选择要修改的学生")
return
student = self.student_listbox.get(selected_index[0])
id, name, phone, email = student.split(",")
new_name = self.name_entry.get()
new_phone = self.phone_entry.get()
new_email = self.email_entry.get()
self.address_book.update_student(id, new_name, new_phone, new_email)
self.load_students()
def delete_student(self):
selected_index = self.student_listbox.curselection()
if len(selected_index) == 0:
messagebox.showwarning("错误", "请选择要删除的学生")
return
student = self.student_listbox.get(selected_index[0])
id, name, phone, email = student.split(",")
result = messagebox.askyesno("确认", f"确定要删除 {name} 吗?")
if result == tk.YES:
self.address_book.delete_student(id)
self.load_students()
def search_student(self):
search_term = self.search_entry.get()
students = self.address_book.search_student(search_term)
self.student_listbox.delete(0, tk.END)
for student in students:
self.student_listbox.insert(tk.END, f"{student.id},{student.name},{student.phone},{student.email}")
def load_students(self):
self.student_listbox.delete(0, tk.END)
students = self.address_book.get_all_students()
for student in students:
self.student_listbox.insert(tk.END, f"{student.id},{student.name},{student.phone},{student.email}")
def run(self):
self.root.mainloop()
if __name__ == "__main__":
app = App()
app.run()
```
该代码使用了SQLite数据库来存储学生和通讯录信息,使用了Tkinter图形界面库来设计用户界面。用户可以添加、修改、删除、搜索学生信息,并且可以在列表框中查看所有学生的信息。
阅读全文