本文主要是介绍Python 实现 账本bill GUI小项目(wxPython),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Python 实现 账本bill GUI小项目
GUI采用wxPython
使用txt文本进行记录和读取数据,按照日期进行快速排序,点击Plot按钮可打印账单并绘制图表实现可视化
效果如下图
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)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!