python用tkinker编写好友管理系统,gui界面,多来几个代码
时间: 2024-03-18 21:39:19 浏览: 62
关于使用tkinter库进行python GUI程序开发
好的,下面是一个使用 tkinter 编写的简单好友管理系统的代码示例:
```python
import tkinter as tk
class Friend:
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
class FriendManagementSystem:
def __init__(self):
self.friends = []
self.selected_friend = None
# 创建主窗口
self.root = tk.Tk()
self.root.title("好友管理系统")
# 创建组件
self.name_label = tk.Label(self.root, text="姓名:")
self.name_entry = tk.Entry(self.root)
self.age_label = tk.Label(self.root, text="年龄:")
self.age_entry = tk.Entry(self.root)
self.gender_label = tk.Label(self.root, text="性别:")
self.gender_entry = tk.Entry(self.root)
self.add_button = tk.Button(self.root, text="添加好友", command=self.add_friend)
self.update_button = tk.Button(self.root, text="修改好友", command=self.update_friend, state=tk.DISABLED)
self.delete_button = tk.Button(self.root, text="删除好友", command=self.delete_friend, state=tk.DISABLED)
self.friend_listbox = tk.Listbox(self.root, selectmode=tk.SINGLE)
self.friend_listbox.bind("<<ListboxSelect>>", self.select_friend)
# 布局组件
self.name_label.grid(row=0, column=0)
self.name_entry.grid(row=0, column=1)
self.age_label.grid(row=1, column=0)
self.age_entry.grid(row=1, column=1)
self.gender_label.grid(row=2, column=0)
self.gender_entry.grid(row=2, column=1)
self.add_button.grid(row=3, column=0)
self.update_button.grid(row=3, column=1)
self.delete_button.grid(row=3, column=2)
self.friend_listbox.grid(row=4, columnspan=3)
# 显示窗口
self.show_friends()
self.root.mainloop()
def show_friends(self):
self.friend_listbox.delete(0, tk.END)
for friend in self.friends:
self.friend_listbox.insert(tk.END, friend.name)
def add_friend(self):
name = self.name_entry.get()
age = self.age_entry.get()
gender = self.gender_entry.get()
friend = Friend(name, age, gender)
self.friends.append(friend)
self.name_entry.delete(0, tk.END)
self.age_entry.delete(0, tk.END)
self.gender_entry.delete(0, tk.END)
self.show_friends()
def update_friend(self):
name = self.name_entry.get()
age = self.age_entry.get()
gender = self.gender_entry.get()
self.selected_friend.name = name
self.selected_friend.age = age
self.selected_friend.gender = gender
self.name_entry.delete(0, tk.END)
self.age_entry.delete(0, tk.END)
self.gender_entry.delete(0, tk.END)
self.show_friends()
def delete_friend(self):
self.friends.remove(self.selected_friend)
self.selected_friend = None
self.name_entry.delete(0, tk.END)
self.age_entry.delete(0, tk.END)
self.gender_entry.delete(0, tk.END)
self.show_friends()
def select_friend(self, event):
index = self.friend_listbox.curselection()[0]
self.selected_friend = self.friends[index]
self.name_entry.delete(0, tk.END)
self.name_entry.insert(0, self.selected_friend.name)
self.age_entry.delete(0, tk.END)
self.age_entry.insert(0, self.selected_friend.age)
self.gender_entry.delete(0, tk.END)
self.gender_entry.insert(0, self.selected_friend.gender)
self.update_button.config(state=tk.NORMAL)
self.delete_button.config(state=tk.NORMAL)
if __name__ == "__main__":
app = FriendManagementSystem()
```
这个程序使用了 tkinter 的各种组件来实现一个简单的好友管理系统,包括添加、修改、删除好友等功能。你可以根据自己的需求修改代码,并添加自己的功能。
阅读全文