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

Python 实现 账本bill GUI小项目

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


Python 实现 账本bill GUI小项目(wxPython)_第1张图片
Python 实现 账本bill GUI小项目(wxPython)_第2张图片


import wx
import matplotlib.pyplot as plt


class PocketTxt:
    def __init__(self, filename):
        self.filename = filename
        self.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.cost

    def 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] = tmp
    tmp = cost[i]
    cost[i] = cost[j]
    cost[j] = tmp


def partition(date, cost, low, high):
    i = low - 1
    j = low
    x = date[high]
    for j in range(low, high+1):
        if date[j] < x:
            i += 1
            swap(date, cost, i, j)
    swap(date, cost, i + 1, j)
    return i + 1


def 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)
    return


class 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 True

    def 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] = 0
        for 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,GUI小项目)