python通讯录管理系统txt用窗体体现

时间: 2023-07-03 21:14:42 浏览: 56
好的,我可以为您提供一些参考代码和思路。首先,您可以使用Python的Tkinter库创建窗体界面。然后,您需要使用Python的文件操作功能将通讯录数据保存到txt文件中,并且在程序启动时读取txt文件中的数据,显示在窗体上。 以下是一个可能的实现方式,仅供参考: ```python import tkinter as tk class Contact: def __init__(self, name, phone): self.name = name self.phone = phone class ContactManager: def __init__(self): self.contacts = [] self.load_contacts() def add_contact(self, name, phone): contact = Contact(name, phone) self.contacts.append(contact) self.save_contacts() def remove_contact(self, contact): self.contacts.remove(contact) self.save_contacts() def get_contacts(self): return self.contacts def load_contacts(self): try: with open("contacts.txt", "r") as f: for line in f.readlines(): name, phone = line.strip().split(",") self.contacts.append(Contact(name, phone)) except FileNotFoundError: pass def save_contacts(self): with open("contacts.txt", "w") as f: for contact in self.contacts: f.write(f"{contact.name},{contact.phone}\n") class ContactWindow: def __init__(self, contact_manager): self.contact_manager = contact_manager self.window = tk.Tk() self.window.geometry("300x300") self.window.title("Contact Manager") self.name_label = tk.Label(self.window, text="Name:") self.name_label.pack() self.name_entry = tk.Entry(self.window) self.name_entry.pack() self.phone_label = tk.Label(self.window, text="Phone:") self.phone_label.pack() self.phone_entry = tk.Entry(self.window) self.phone_entry.pack() self.add_button = tk.Button(self.window, text="Add Contact", command=self.add_contact) self.add_button.pack() self.contacts_label = tk.Label(self.window, text="Contacts:") self.contacts_label.pack() self.contacts_listbox = tk.Listbox(self.window) self.contacts_listbox.pack() self.remove_button = tk.Button(self.window, text="Remove Contact", command=self.remove_contact) self.remove_button.pack() self.update_contacts() self.window.mainloop() def update_contacts(self): self.contacts_listbox.delete(0, tk.END) contacts = self.contact_manager.get_contacts() for contact in contacts: self.contacts_listbox.insert(tk.END, f"{contact.name} ({contact.phone})") def add_contact(self): name = self.name_entry.get() phone = self.phone_entry.get() if name and phone: self.contact_manager.add_contact(name, phone) self.update_contacts() def remove_contact(self): selection = self.contacts_listbox.curselection() if selection: index = selection[0] contact = self.contact_manager.get_contacts()[index] self.contact_manager.remove_contact(contact) self.update_contacts() if __name__ == "__main__": contact_manager = ContactManager() contact_window = ContactWindow(contact_manager) ``` 在这个示例中,我们首先定义了一个Contact类来表示一个联系人。然后,我们创建了一个ContactManager类来管理联系人列表,并且在程序启动时从txt文件中读取联系人数据。接着,我们使用Tkinter库创建了一个ContactWindow类来显示通讯录的窗体界面,并且在窗体中添加了文本框、按钮和列表框等控件来实现添加、删除和显示联系人的功能。最后,我们在主程序中创建了一个ContactManager对象和一个ContactWindow对象来运行整个程序。 请注意,这只是一个简单的示例,实际的窗体界面和通讯录管理功能需要根据您的具体需求进行修改和完善。

相关推荐

最新推荐

recommend-type

使用Python实现 学生学籍管理系统

主要介绍了使用Python实现 学生学籍管理系统,代码大致分为五个函数组成,具体内容详情本文给大家介绍的非常详细,需要的朋友可以参考下
recommend-type

python学生信息管理系统实现代码

主要介绍了python学生信息管理系统的实现代码,代码简单,复制即可使用,需要的朋友可以参考下
recommend-type

python实现超市商品销售管理系统

主要为大家详细介绍了python实现超市商品销售管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

Python实现学校管理系统

主要为大家详细介绍了Python实现学校管理系统,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

python实现外卖信息管理系统

主要为大家详细介绍了python实现外卖信息管理系统,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

用 Python 画一个可以动的爱心

好的,我可以帮助您使用Python来绘制一个动态的爱心。您可以使用turtle库来实现。以下是实现代码: ```python import turtle import math # 设置画布和画笔 canvas = turtle.Screen() canvas.bgcolor("black") pencil = turtle.Turtle() pencil.speed(0) pencil.color("red", "pink") pencil.pensize(3) # 定义爱心函数 def draw_love(heart_size, x_offset=0, y_offset=0):
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。