好友管理系统python代码 thinker
时间: 2023-11-21 07:05:33 浏览: 93
以下是一个使用 Tkinter 图形用户界面的好友管理系统的 Python 代码示例:
```python
import tkinter as tk
from tkinter import ttk
class Friend:
def __init__(self, name, age, gender, phone):
self.name = name
self.age = age
self.gender = gender
self.phone = phone
class FriendList:
def __init__(self):
self.friend_list = []
def add_friend(self, friend):
self.friend_list.append(friend)
def remove_friend(self, friend):
self.friend_list.remove(friend)
def search_friend(self, name):
for friend in self.friend_list:
if friend.name == name:
return friend
return None
def display_all_friends(self):
friend_info = []
for friend in self.friend_list:
friend_info.append((friend.name, friend.age, friend.gender, friend.phone))
return friend_info
class FriendManager:
def __init__(self, root, friend_list):
self.root = root
self.friend_list = friend_list
self.friend_name = tk.StringVar()
self.friend_age = tk.StringVar()
self.friend_gender = tk.StringVar()
self.friend_phone = tk.StringVar()
self.create_widgets()
def create_widgets(self):
# 创建标签和输入框
name_label = ttk.Label(self.root, text="Name:")
name_label.grid(row=0, column=0, padx=5, pady=5)
name_entry = ttk.Entry(self.root, textvariable=self.friend_name)
name_entry.grid(row=0, column=1, padx=5, pady=5)
age_label = ttk.Label(self.root, text="Age:")
age_label.grid(row=1, column=0, padx=5, pady=5)
age_entry = ttk.Entry(self.root, textvariable=self.friend_age)
age_entry.grid(row=1, column=1, padx=5, pady=5)
gender_label = ttk.Label(self.root, text="Gender:")
gender_label.grid(row=2, column=0, padx=5, pady=5)
gender_entry = ttk.Entry(self.root, textvariable=self.friend_gender)
gender_entry.grid(row=2, column=1, padx=5, pady=5)
phone_label = ttk.Label(self.root, text="Phone:")
phone_label.grid(row=3, column=0, padx=5, pady=5)
phone_entry = ttk.Entry(self.root, textvariable=self.friend_phone)
phone_entry.grid(row=3, column=1, padx=5, pady=5)
# 创建按钮
add_button = ttk.Button(self.root, text="Add Friend", command=self.add_friend)
add_button.grid(row=4, column=0, padx=5, pady=5)
remove_button = ttk.Button(self.root, text="Remove Friend", command=self.remove_friend)
remove_button.grid(row=4, column=1, padx=5, pady=5)
search_button = ttk.Button(self.root, text="Search Friend", command=self.search_friend)
search_button.grid(row=4, column=2, padx=5, pady=5)
display_button = ttk.Button(self.root, text="Display All Friends", command=self.display_all_friends)
display_button.grid(row=5, column=0, padx=5, pady=5)
# 创建列表框
self.friend_listbox = tk.Listbox(self.root)
self.friend_listbox.grid(row=6, columnspan=3, padx=5, pady=5)
def add_friend(self):
friend = Friend(self.friend_name.get(), self.friend_age.get(), self.friend_gender.get(), self.friend_phone.get())
self.friend_list.add_friend(friend)
self.friend_listbox.insert("end", friend.name)
def remove_friend(self):
friend_name = self.friend_name.get()
friend = self.friend_list.search_friend(friend_name)
if friend:
self.friend_list.remove_friend(friend)
self.friend_listbox.delete("active")
def search_friend(self):
friend_name = self.friend_name.get()
friend = self.friend_list.search_friend(friend_name)
if friend:
self.friend_age.set(friend.age)
self.friend_gender.set(friend.gender)
self.friend_phone.set(friend.phone)
else:
self.friend_age.set("")
self.friend_gender.set("")
self.friend_phone.set("")
def display_all_friends(self):
self.friend_listbox.delete(0, "end")
friend_info = self.friend_list.display_all_friends()
for name, age, gender, phone in friend_info:
self.friend_listbox.insert("end", name)
if __name__ == "__main__":
root = tk.Tk()
root.title("Friend Manager")
friend_list = FriendList()
friend_manager = FriendManager(root, friend_list)
root.mainloop()
```
该代码使用 Tkinter 创建了一个简单的图形用户界面,其中包含添加、删除、搜索和显示所有好友的功能。在主程序中,我们创建了一个 FriendList 对象和 FriendManager 对象,然后启动了 Tkinter 主循环。在 FriendManager 类中,我们创建了标签、输入框、按钮和列表框,然后实现了添加、删除、搜索和显示所有好友的方法。这些方法会更新 FriendList 对象和 FriendManager 对象中的数据,并在列表框中显示好友信息。
阅读全文