python使用tkinter模块写名片管理系统,实现增删查改
时间: 2023-07-20 14:31:31 浏览: 88
基于Python的学生信息管理系统 学生教师端分离,支持数据的增删查改、数据分析与统计 采用Tkinter实现图形界面
好的,下面是一个简单的名片管理系统的实现示例:
```
import tkinter as tk
class Card:
def __init__(self, name, phone):
self.name = name
self.phone = phone
class CardManager:
def __init__(self):
self.cards = []
def add_card(self, card):
self.cards.append(card)
def remove_card(self, card):
self.cards.remove(card)
def search_card(self, name):
for card in self.cards:
if card.name == name:
return card
return None
def update_card(self, card, name, phone):
card.name = name
card.phone = phone
class CardGUI:
def __init__(self):
self.manager = CardManager()
self.window = tk.Tk()
self.window.title("名片管理系统")
self.create_widgets()
def create_widgets(self):
# 新增名片
self.add_frame = tk.Frame(self.window)
self.add_frame.pack(side=tk.TOP, padx=10, pady=10)
tk.Label(self.add_frame, text="姓名:").grid(row=0, column=0)
self.add_name_entry = tk.Entry(self.add_frame)
self.add_name_entry.grid(row=0, column=1)
tk.Label(self.add_frame, text="电话:").grid(row=1, column=0)
self.add_phone_entry = tk.Entry(self.add_frame)
self.add_phone_entry.grid(row=1, column=1)
tk.Button(self.add_frame, text="新增", command=self.add_card).grid(row=2, column=1)
# 删除名片
self.remove_frame = tk.Frame(self.window)
self.remove_frame.pack(side=tk.TOP, padx=10, pady=10)
tk.Label(self.remove_frame, text="姓名:").grid(row=0, column=0)
self.remove_name_entry = tk.Entry(self.remove_frame)
self.remove_name_entry.grid(row=0, column=1)
tk.Button(self.remove_frame, text="删除", command=self.remove_card).grid(row=1, column=1)
# 查询名片
self.search_frame = tk.Frame(self.window)
self.search_frame.pack(side=tk.TOP, padx=10, pady=10)
tk.Label(self.search_frame, text="姓名:").grid(row=0, column=0)
self.search_name_entry = tk.Entry(self.search_frame)
self.search_name_entry.grid(row=0, column=1)
self.search_result_label = tk.Label(self.search_frame, text="")
self.search_result_label.grid(row=1, column=1)
tk.Button(self.search_frame, text="查询", command=self.search_card).grid(row=2, column=1)
# 修改名片
self.update_frame = tk.Frame(self.window)
self.update_frame.pack(side=tk.TOP, padx=10, pady=10)
tk.Label(self.update_frame, text="原姓名:").grid(row=0, column=0)
self.update_old_name_entry = tk.Entry(self.update_frame)
self.update_old_name_entry.grid(row=0, column=1)
tk.Label(self.update_frame, text="新姓名:").grid(row=1, column=0)
self.update_new_name_entry = tk.Entry(self.update_frame)
self.update_new_name_entry.grid(row=1, column=1)
tk.Label(self.update_frame, text="新电话:").grid(row=2, column=0)
self.update_new_phone_entry = tk.Entry(self.update_frame)
self.update_new_phone_entry.grid(row=2, column=1)
tk.Button(self.update_frame, text="修改", command=self.update_card).grid(row=3, column=1)
def add_card(self):
name = self.add_name_entry.get()
phone = self.add_phone_entry.get()
card = Card(name, phone)
self.manager.add_card(card)
self.add_name_entry.delete(0, tk.END)
self.add_phone_entry.delete(0, tk.END)
def remove_card(self):
name = self.remove_name_entry.get()
card = self.manager.search_card(name)
if card:
self.manager.remove_card(card)
self.remove_name_entry.delete(0, tk.END)
else:
self.remove_name_entry.delete(0, tk.END)
self.remove_name_entry.insert(0, "不存在该名片")
def search_card(self):
name = self.search_name_entry.get()
card = self.manager.search_card(name)
if card:
self.search_result_label.config(text="姓名:{},电话:{}".format(card.name, card.phone))
self.search_name_entry.delete(0, tk.END)
else:
self.search_result_label.config(text="不存在该名片")
self.search_name_entry.delete(0, tk.END)
def update_card(self):
old_name = self.update_old_name_entry.get()
new_name = self.update_new_name_entry.get()
new_phone = self.update_new_phone_entry.get()
card = self.manager.search_card(old_name)
if card:
self.manager.update_card(card, new_name, new_phone)
self.update_old_name_entry.delete(0, tk.END)
self.update_new_name_entry.delete(0, tk.END)
self.update_new_phone_entry.delete(0, tk.END)
else:
self.update_old_name_entry.delete(0, tk.END)
self.update_old_name_entry.insert(0, "不存在该名片")
def run(self):
self.window.mainloop()
if __name__ == "__main__":
gui = CardGUI()
gui.run()
```
这个程序使用了 `tkinter` 模块创建了一个 GUI 界面,在界面上提供了新增、删除、查询和修改名片的功能。其中,`Card` 类表示一个名片,`CardManager` 类负责管理所有名片,`CardGUI` 类负责创建 GUI 界面并与 `CardManager` 类交互来实现各种名片操作。
阅读全文