在python中用thinker编写好友管理系统 带窗口的
时间: 2024-03-14 20:45:23 浏览: 51
好的,下面是一个简单的好友管理系统的Python代码,使用了Tkinter库来创建GUI窗口。这个程序可以添加、删除和查找好友,具体代码如下:
```python
from tkinter import *
class Friend:
def __init__(self, name, phone, email):
self.name = name
self.phone = phone
self.email = email
class FriendManager:
def __init__(self):
self.friends = []
def add_friend(self, friend):
self.friends.append(friend)
def remove_friend(self, friend):
self.friends.remove(friend)
def search_friend(self, name):
for friend in self.friends:
if friend.name == name:
return friend
return None
class App:
def __init__(self, master, manager):
self.master = master
self.manager = manager
self.master.title("Friend Manager")
self.label_name = Label(self.master, text="Name:")
self.label_name.grid(row=0, column=0)
self.entry_name = Entry(self.master)
self.entry_name.grid(row=0, column=1)
self.label_phone = Label(self.master, text="Phone:")
self.label_phone.grid(row=1, column=0)
self.entry_phone = Entry(self.master)
self.entry_phone.grid(row=1, column=1)
self.label_email = Label(self.master, text="Email:")
self.label_email.grid(row=2, column=0)
self.entry_email = Entry(self.master)
self.entry_email.grid(row=2, column=1)
self.button_add = Button(self.master, text="Add", command=self.add_friend)
self.button_add.grid(row=3, column=0)
self.button_remove = Button(self.master, text="Remove", command=self.remove_friend)
self.button_remove.grid(row=3, column=1)
self.button_search = Button(self.master, text="Search", command=self.search_friend)
self.button_search.grid(row=3, column=2)
self.text_area = Text(self.master, height=10, width=30)
self.text_area.grid(row=4, column=0, columnspan=3)
def add_friend(self):
name = self.entry_name.get()
phone = self.entry_phone.get()
email = self.entry_email.get()
friend = Friend(name, phone, email)
self.manager.add_friend(friend)
self.text_area.delete("1.0", END)
self.text_area.insert(END, "Added: {}\n".format(friend.name))
def remove_friend(self):
name = self.entry_name.get()
friend = self.manager.search_friend(name)
if friend:
self.manager.remove_friend(friend)
self.text_area.delete("1.0", END)
self.text_area.insert(END, "Removed: {}\n".format(friend.name))
else:
self.text_area.delete("1.0", END)
self.text_area.insert(END, "Friend not found!\n")
def search_friend(self):
name = self.entry_name.get()
friend = self.manager.search_friend(name)
if friend:
self.text_area.delete("1.0", END)
self.text_area.insert(END, "Name: {}\nPhone: {}\nEmail: {}\n".format(friend.name, friend.phone, friend.email))
else:
self.text_area.delete("1.0", END)
self.text_area.insert(END, "Friend not found!\n")
manager = FriendManager()
root = Tk()
app = App(root, manager)
root.mainloop()
```
这个程序创建了一个Friend类,用于存储好友信息,以及一个FriendManager类,用于管理好友列表。App类是一个GUI窗口,包括一个添加好友、删除好友和查找好友的表单,以及一个文本框用于显示结果。运行这个程序,会出现一个窗口,您可以在窗口中添加、删除和查找好友。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)