Python 实现 账本bill GUI小项目(wxPython)

2023-10-13 02:40

本文主要是介绍Python 实现 账本bill GUI小项目(wxPython),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Python 实现 账本bill GUI小项目

GUI采用wxPython
使用txt文本进行记录和读取数据,按照日期进行快速排序,点击Plot按钮可打印账单并绘制图表实现可视化
效果如下图


GUI界面
可视化效果


import wx
import matplotlib.pyplot as pltclass PocketTxt:def __init__(self, filename):self.filename = filenameself.time = []self.cost = []def read(self):txt = open(self.filename, 'r')self.time = []self.cost = []for line in txt:data = line.split()self.time.append(data[0])self.cost.append(float(data[1]))txt.close()return self.time, self.costdef update(self): self.read()def write(self, date, cost):txt = open(self.filename, 'a')data = "\n" + str(date) + " " + str(cost)txt.write(data)txt.close()def swap(date, cost, i, j):tmp = date[i]date[i] = date[j]date[j] = tmptmp = cost[i]cost[i] = cost[j]cost[j] = tmpdef partition(date, cost, low, high):i = low - 1j = lowx = date[high]for j in range(low, high+1):if date[j] < x:i += 1swap(date, cost, i, j)swap(date, cost, i + 1, j)return i + 1def quick_sort(date, cost, low, high):if low < high:mid = partition(date, cost, low, high)quick_sort(date, cost, low, mid - 1)quick_sort(date, cost, mid + 1, high)returnclass PocketFrame(wx.Frame):def __init__(self, *args, **kw):super(PocketFrame, self).__init__(*args, **kw)self.panel = wx.Panel(self)self.font = wx.Font(20, wx.DEFAULT, wx.NORMAL, wx.NORMAL, False, "SimHei")self.title = wx.StaticText(parent=self.panel,label="Enter date and cost\nFormat: 20190220 233",pos=(160, 70))self.title.SetFont(self.font)self.date_label = wx.StaticText(parent=self.panel, label="Enter date:", pos=(110, 135))self.date_label.SetFont(self.font)self.date_text = wx.TextCtrl(parent=self.panel, pos=(280, 140), size=(200, 25), style=wx.CENTER)self.cost_label = wx.StaticText(parent=self.panel, label="Enter cost:", pos=(110, 180))self.cost_label.SetFont(self.font)self.cost_text = wx.TextCtrl(parent=self.panel, pos=(280, 185), size=(200, 25), style=wx.CENTER)self.confirm = wx.Button(parent=self.panel, label='Confirm', pos=(110, 235), size=(120, 40), style=wx.CENTER)self.confirm.SetFont(self.font)self.cancel = wx.Button(parent=self.panel, label='Cancel', pos=(350, 235), size=(120, 40), style=wx.CENTER)self.cancel.SetFont(self.font)self.plot = wx.Button(parent=self.panel, label='Plot', pos=(228,280), size=(120, 40), style=wx.CENTER)self.plot.SetFont(self.font)self.confirm.Bind(wx.EVT_BUTTON, self.OnClickConfirm)self.cancel.Bind(wx.EVT_BUTTON, self.OnClickCancel)self.plot.Bind(wx.EVT_BUTTON, self.OnClickPlot)self.txt = PocketTxt('record/lhy.txt')self.raw_time, self.raw_cost = self.txt.read()self.date = []self.cost = []def update_date(self):self.raw_time, self.raw_cost = self.txt.read()self.data_sort()def CheckFormat(self, date, cost):return Truedef OnClickConfirm(self, event):message = ""date = self.date_text.GetValue()cost = self.cost_text.GetValue()if date == "" and cost == "":message = "Please enter data first!"elif date == "" and (not cost == ""):message = "Please enter date!"elif cost == "" and (not date == ""):message = "Please enter cost!"elif self.CheckFormat(date, cost):self.txt.write(date, cost)message = 'Record Succussfully!'else:message = 'Format Error!'wx.MessageBox(message)def OnClickCancel(self, event):self.date_text.SetValue("")self.cost_text.SetValue("")def OnClickPlot(self, event):self.show_bill()def data_sort(self):t_set = set(self.raw_time)data = {}for t in t_set:data[t] = 0for i in range(0, len(self.raw_time)):data[self.raw_time[i]] += self.raw_cost[i]key = data.keys()value = data.values()self.date = [str(k) for k in key]self.cost = [float(v) for v in value]quick_sort(self.date, self.cost, 0, len(self.date) - 1)def show_bill(self):self.update_date()for i in range(0, len(self.date)):print(self.date[i], self.cost[i])plt.plot(self.date, self.cost)plt.figure()plt.bar(self.date, self.cost)plt.show()class PocketBook:def __init__(self):self.app = wx.App()self.frame = PocketFrame(None, title='Pocket', size=(600, 450))def run(self):self.frame.Show()self.app.MainLoop()if __name__ == '__main__':pocket_book = PocketBook()pocket_book.run()

这篇关于Python 实现 账本bill GUI小项目(wxPython)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/200193

相关文章

如何使用 Python 读取 Excel 数据

《如何使用Python读取Excel数据》:本文主要介绍使用Python读取Excel数据的详细教程,通过pandas和openpyxl,你可以轻松读取Excel文件,并进行各种数据处理操... 目录使用 python 读取 Excel 数据的详细教程1. 安装必要的依赖2. 读取 Excel 文件3. 读

Go语言开发实现查询IP信息的MCP服务器

《Go语言开发实现查询IP信息的MCP服务器》随着MCP的快速普及和广泛应用,MCP服务器也层出不穷,本文将详细介绍如何在Go语言中使用go-mcp库来开发一个查询IP信息的MCP... 目录前言mcp-ip-geo 服务器目录结构说明查询 IP 信息功能实现工具实现工具管理查询单个 IP 信息工具的实现服

Python的time模块一些常用功能(各种与时间相关的函数)

《Python的time模块一些常用功能(各种与时间相关的函数)》Python的time模块提供了各种与时间相关的函数,包括获取当前时间、处理时间间隔、执行时间测量等,:本文主要介绍Python的... 目录1. 获取当前时间2. 时间格式化3. 延时执行4. 时间戳运算5. 计算代码执行时间6. 转换为指

利用Python调试串口的示例代码

《利用Python调试串口的示例代码》在嵌入式开发、物联网设备调试过程中,串口通信是最基础的调试手段本文将带你用Python+ttkbootstrap打造一款高颜值、多功能的串口调试助手,需要的可以了... 目录概述:为什么需要专业的串口调试工具项目架构设计1.1 技术栈选型1.2 关键类说明1.3 线程模

SpringBoot基于配置实现短信服务策略的动态切换

《SpringBoot基于配置实现短信服务策略的动态切换》这篇文章主要为大家详细介绍了SpringBoot在接入多个短信服务商(如阿里云、腾讯云、华为云)后,如何根据配置或环境切换使用不同的服务商,需... 目录目标功能示例配置(application.yml)配置类绑定短信发送策略接口示例:阿里云 & 腾

Python ZIP文件操作技巧详解

《PythonZIP文件操作技巧详解》在数据处理和系统开发中,ZIP文件操作是开发者必须掌握的核心技能,Python标准库提供的zipfile模块以简洁的API和跨平台特性,成为处理ZIP文件的首选... 目录一、ZIP文件操作基础三板斧1.1 创建压缩包1.2 解压操作1.3 文件遍历与信息获取二、进阶技

SpringBoot项目中报错The field screenShot exceeds its maximum permitted size of 1048576 bytes.的问题及解决

《SpringBoot项目中报错ThefieldscreenShotexceedsitsmaximumpermittedsizeof1048576bytes.的问题及解决》这篇文章... 目录项目场景问题描述原因分析解决方案总结项目场景javascript提示:项目相关背景:项目场景:基于Spring

Python Transformers库(NLP处理库)案例代码讲解

《PythonTransformers库(NLP处理库)案例代码讲解》本文介绍transformers库的全面讲解,包含基础知识、高级用法、案例代码及学习路径,内容经过组织,适合不同阶段的学习者,对... 目录一、基础知识1. Transformers 库简介2. 安装与环境配置3. 快速上手示例二、核心模

解决Maven项目idea找不到本地仓库jar包问题以及使用mvn install:install-file

《解决Maven项目idea找不到本地仓库jar包问题以及使用mvninstall:install-file》:本文主要介绍解决Maven项目idea找不到本地仓库jar包问题以及使用mvnin... 目录Maven项目idea找不到本地仓库jar包以及使用mvn install:install-file基

Python正则表达式语法及re模块中的常用函数详解

《Python正则表达式语法及re模块中的常用函数详解》这篇文章主要给大家介绍了关于Python正则表达式语法及re模块中常用函数的相关资料,正则表达式是一种强大的字符串处理工具,可以用于匹配、切分、... 目录概念、作用和步骤语法re模块中的常用函数总结 概念、作用和步骤概念: 本身也是一个字符串,其中