帮我编写基于Python语言的桌面应用程序:个人记账系统的代码 具体要求: (1)使用类实现数据封装; (2)使用tkinter实现界面UI设计。 (3)使用文件保存数据。 (4)能实现用户登陆注册功能和实现数据的增加、修改、删除和查询操作。 (5)开发平台:Python 3.7以上版本等软件。 (6)项目选题:开题前需向任何教师确认题目及开发难度是否符合课程要求。 (7)作品要求:界面美观易操作,人机交互符合商业应用流程。

时间: 2024-03-10 14:45:19 浏览: 13
好的,以下是一个简单的个人记账系统的Python代码,使用了类实现数据封装和tkinter实现界面UI设计,同时使用文件保存数据。代码中实现了用户的登陆注册功能以及数据的增加、修改、删除和查询操作。请注意,这个代码只是一个示例,可能需要根据具体需求进行修改和调整。 ```python import tkinter as tk import tkinter.messagebox as messagebox import pickle class Account(): def __init__(self, name, amount): self.name = name self.amount = amount class User(): def __init__(self, username, password): self.username = username self.password = password self.accounts = [] def add_account(self, account): self.accounts.append(account) def remove_account(self, account): self.accounts.remove(account) def modify_account(self, account, name, amount): account.name = name account.amount = amount class AccountSystem(): def __init__(self): self.users = [] def register(self, username, password): for user in self.users: if user.username == username: messagebox.showwarning('注册失败', '该用户名已经被注册!') return self.users.append(User(username, password)) messagebox.showinfo('注册成功', '恭喜,注册成功!') def login(self, username, password): for user in self.users: if user.username == username and user.password == password: return user return None def save_data(self): with open('data.pickle', 'wb') as f: pickle.dump(self.users, f) def load_data(self): try: with open('data.pickle', 'rb') as f: self.users = pickle.load(f) except FileNotFoundError: pass class AccountSystemUI(): def __init__(self): self.account_system = AccountSystem() self.account_system.load_data() self.current_user = None self.window = tk.Tk() self.window.title('个人记账系统') self.window.geometry('400x400') self.username_label = tk.Label(self.window, text='用户名:') self.username_label.pack() self.username_entry = tk.Entry(self.window) self.username_entry.pack() self.password_label = tk.Label(self.window, text='密码:') self.password_label.pack() self.password_entry = tk.Entry(self.window, show='*') self.password_entry.pack() self.login_button = tk.Button(self.window, text='登录', command=self.login) self.login_button.pack() self.register_button = tk.Button(self.window, text='注册', command=self.register) self.register_button.pack() self.account_name_label = tk.Label(self.window, text='账户名称:') self.account_name_label.pack() self.account_name_entry = tk.Entry(self.window) self.account_name_entry.pack() self.account_amount_label = tk.Label(self.window, text='账户余额:') self.account_amount_label.pack() self.account_amount_entry = tk.Entry(self.window) self.account_amount_entry.pack() self.add_button = tk.Button(self.window, text='增加账户', command=self.add_account, state=tk.DISABLED) self.add_button.pack() self.modify_button = tk.Button(self.window, text='修改账户', command=self.modify_account, state=tk.DISABLED) self.modify_button.pack() self.remove_button = tk.Button(self.window, text='删除账户', command=self.remove_account, state=tk.DISABLED) self.remove_button.pack() self.query_button = tk.Button(self.window, text='查询账户', command=self.query_account, state=tk.DISABLED) self.query_button.pack() def login(self): username = self.username_entry.get() password = self.password_entry.get() user = self.account_system.login(username, password) if user is not None: self.current_user = user self.username_entry.config(state=tk.DISABLED) self.password_entry.config(state=tk.DISABLED) self.login_button.config(state=tk.DISABLED) self.register_button.config(state=tk.DISABLED) self.add_button.config(state=tk.NORMAL) self.modify_button.config(state=tk.NORMAL) self.remove_button.config(state=tk.NORMAL) self.query_button.config(state=tk.NORMAL) messagebox.showinfo('登录成功', '欢迎回来,{}!'.format(username)) else: messagebox.showwarning('登录失败', '用户名或密码错误!') def register(self): username = self.username_entry.get() password = self.password_entry.get() self.account_system.register(username, password) def add_account(self): name = self.account_name_entry.get() amount = float(self.account_amount_entry.get()) account = Account(name, amount) self.current_user.add_account(account) self.account_system.save_data() messagebox.showinfo('增加账户', '账户{}已经成功增加!'.format(name)) def modify_account(self): account = self.get_selected_account() if account is None: return name = self.account_name_entry.get() amount = float(self.account_amount_entry.get()) self.current_user.modify_account(account, name, amount) self.account_system.save_data() messagebox.showinfo('修改账户', '账户{}已经成功修改!'.format(name)) def remove_account(self): account = self.get_selected_account() if account is None: return self.current_user.remove_account(account) self.account_system.save_data() messagebox.showinfo('删除账户', '账户{}已经成功删除!'.format(account.name)) def query_account(self): account = self.get_selected_account() if account is None: return messagebox.showinfo('查询账户', '账户名称:{}\n账户余额:{}'.format(account.name, account.amount)) def get_selected_account(self): selection = self.accounts_list.curselection() if len(selection) == 0: messagebox.showwarning('操作失败', '请先选择一个账户!') return None index = selection[0] return self.current_user.accounts[index] def run(self): self.window.mainloop() ui = AccountSystemUI() ui.run() ``` 希望这个代码能对你有所帮助,如果有任何问题或疑问,欢迎随时提出。

相关推荐

最新推荐

recommend-type

Java与Python之间使用jython工具类实现数据交互

今天小编就为大家分享一篇关于Java与Python之间使用jython工具类实现数据交互,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
recommend-type

[Python语言程序设计-第11期] 测验1: Python基本语法元素 (第1周)

2.以下关于Python语言中“缩进”说法正确的是:缩进在程序中长度统一且强制使用 3.以下不属于IPO模型的是:Program 4.字符串是一个字符序列,给字符串s,以下表示s从右侧向左第三个字符的是:s[-3] 5.以下不是Python...
recommend-type

Python3实现的Mysql数据库操作封装类

主要介绍了Python3实现的Mysql数据库操作封装类,涉及Python针对mysql数据库的连接、查询、更新及关闭连接等相关操作技巧,需要的朋友可以参考下
recommend-type

使用Python做垃圾分类的原理及实例代码附

主要介绍了用Python做垃圾分类的实现原理,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值 ,需要的朋友可以参考下
recommend-type

详解python实现数据归一化处理的方式:(0,1)标准化

主要介绍了详解python实现数据归一化处理的方式:(0,1)标准化,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
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

SPDK_NVMF_DISCOVERY_NQN是什么 有什么作用

SPDK_NVMF_DISCOVERY_NQN 是 SPDK (Storage Performance Development Kit) 中用于查询 NVMf (Non-Volatile Memory express over Fabrics) 存储设备名称的协议。NVMf 是一种基于网络的存储协议,可用于连接远程非易失性内存存储器。 SPDK_NVMF_DISCOVERY_NQN 的作用是让存储应用程序能够通过 SPDK 查询 NVMf 存储设备的名称,以便能够访问这些存储设备。通过查询 NVMf 存储设备名称,存储应用程序可以获取必要的信息,例如存储设备的IP地址、端口号、名称等,以便能
recommend-type

JSBSim Reference Manual

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