Tkinter实例:家庭账本(Python)

Tkinter实例:家庭账本(Python),第1张

目录

0.创作背景

1.主要目标

1.1记录功能:

1.2查询功能:

1.3统计功能:

2.实现过程

2.1开始界面:

2.2记账内容:

2.3查询:

2.4统计:

3.结尾:

3.1 总代码:

3.2前导代码以及其他前导内容:

3.2.1前导代码:

3.2.2前导图片:

3.3结尾:

0.创作背景

疫情在家好无聊,做个账本玩玩。

1.主要目标 1.1记录功能:
  • 主要记账内容为:日期、金额、事项、具体事项等。
  • 要实现事项的按钮选择。
  • 日期设置默认值,默认为当天日期。
  • 收支内容根据事项自动设置。
1.2查询功能:
  • 可以根据日期、金额、事项、具体事项进行查询,其中事项为必填项。
  • 查询后按照记账顺序显示。
  • 没有填写的查询条件默认为不做要求。
1.3统计功能:
  • 根据起始日期和终止日期进行统计。
  • 主要内容包括事项,总金额,百分比等。
2.实现过程 2.1开始界面:

首先,我们需要有一个开始界面。

经过深思熟虑之后,我崩溃的发现,似乎好像也许说不定 开始界面只要三个按钮😭😭😭。这要怎么设计啊?!。

最终我决定用图片凑面积。

我真是太聪明了。

于是乎,开始界面就被我做成了这样:

我个人认为还算美观。有更好建议的欢迎留言。

下面分享一下代码:

from tkinter import * #构建页面

root = Tk() #生成窗口
root.title("家庭账本") #标题
root.geometry("500x500") #大小
root.resizable(width = False, height = False) #不可改变大小

wri = Button(root, text = '记账', command = click) #设置按钮
wri.grid(row = 0, column = 0)
rea = Button(root, text = '查询', command = look_f) #查询键
rea.grid(row = 0, column = 1)
get = Button(root, text = '生成报告', command = know) #统计键
get.grid(row = 0, column = 2)

can = Canvas(root, width = 430, height = 450)
pla = PhotoImage(file = "存钱罐.gif")
pic = can.create_image(250, 250, image=pla)
can.grid(row = 1, columnspan = 3)

root.mainloop()

这样就算勉勉强强做了一个开始界面。

当然啦,你如果光运行这段代码是没有用滴!!!毕竟很多东西都没有写,click, look_f和know都还没有定义!

2.2记账内容:

账本当然要能记账啦。接下来我们一起做一做记账界面吧。

记账需要的内容主要有:分类,事项,金额,日期。

其中,分类可供用户选择,日期可以自动搜索,其他的就需要用户自己输入啦。

于是乎,记账界面便做好了。

有以下几个注意点:

  1. 要防止用户漏填内容。
  2. 要注意日期可以选填。
  3. 输入完成后自动清空,并打开提示窗口。
  4. 收入支出不要搞混。→ 所以,事项分成了收入和支出两个按钮。

展示一下代码:

def click():
    wind = Toplevel(root) #子窗口
    wind.title("记账")
    wind.geometry("300x200")
    wind.resizable(width = False, height = False)

    def without(s): #保存时的提示语
        done = Toplevel(wind)
        if s == None:
            temp = Label(done, text = "保存完毕,请继续")
            temp.grid(row = 0)
        else:
            temp = Label(done, text = "请填写" + s)
            temp.grid(row = 0)
        did = partial(done.destroy)
        but = Button(done, text = "确定", command = did)
        but.grid(row = 1)

    def qd(): #保存记账,并继续
        global w1, w, kind
        w1 = str(g.get())
        if w1 == "":
            without("事项")
            return
        wh = [w, w1]
        cost = str(c_g.get())
        if cost == "":
            without("金额")
            return
        t1 = str(t_g.get())
        if t1 != "":
            t = ""
            for i in t1:
                if i >= '0' and i <= '9':
                    t += i
                else:
                    t += " "
        else:
            t = strftime("%Y %m %d", localtime())
        if kind == "支出":
            cost = '-' + cost
        pf = open("家庭账本", "br")
        ls = load(pf)
        pf.close()
        ls.append([t, wh, cost])
        pf = open("家庭账本", "bw")
        dump(ls, pf)
        pf.close()
        g.delete(0, "end")
        c_g.delete(0, "end")
        t_g.delete(0, "end")
        w1 = ""
        without(None)

    def clo(): #关闭窗口
        wind.destroy()
    
    def do(what): #设置事项
        global w
        w = what
        out = Label(wind, text = "事项: " + w)
        out.grid(row = 0, column = 0)
        if w in "饮食娱乐购物学习其他":
            do_k("支出")
        elif w == "其他 ":
            do_k("收入")
        else:
            do_k("收入")

    def do_k(what): #设置操作
        global kind
        kind = what
        out_k = Label(wind, text = "操作类型: " + kind)
        out_k.grid(row = 0, column = 1)

    def do_t(): #设置时间
        out_t = Label(wind, text = "默认时间: " + strftime("%Y %m %d", localtime()))
        out_t.grid(row = 1, column = 0)
    
    do("其他")
    do_k("支出")
    do_t()
    
    #菜单条
    menubar = Menu(wind)
    wind.config(menu=menubar)

    #菜单
    m = Menu(wind)
    for i in ["饮食", "娱乐", "购物", "学习", "其他"]:
        if i == "其他":
            m.add_separator() #添加分割线
        do1 = partial(do, i)
        m.add_command(label = i, command = do1)
    menubar.add_cascade(label = "支出", menu = m)

    out = Menu(wind)
    for i in ["工资", "福利", "理财", "其他 "]:
        if i == "其他 ":
            out.add_separator() #添加分割线
        do1 = partial(do, i)
        out.add_command(label = i, command = do1)
    menubar.add_cascade(label = "收入", menu = out)
    
    l = Label(wind, text = "请输入具体事项:", bg = "light sky blue", fg = "white")#提示语
    l.grid(row = 2, column = 0)#行、列、位置(NSWE)
    g = Entry(wind) #文本框
    g.grid(row = 2, column = 1)

    c_l = Label(wind, text = "请输入金额:", bg = "sky blue", fg = "white")
    c_l.grid(row = 3, column = 0)
    c_g = Entry(wind)
    c_g.grid(row = 3, column = 1)

    t_l = Label(wind, text = "请输入时间:", bg = "deep sky blue", fg = "white")
    t_l.grid(row = 4, column = 0)
    t_g = Entry(wind)
    t_g.grid(row = 4, column = 1)

    ok = Button(wind, text = "确定", command = qd)
    close = Button(wind, text = "取消", command = clo)
    temp = Label(wind, text = "  ")
    temp.grid(row = 5)
    ok.grid(row = 6, column = 0)
    close.grid(row = 6, column = 1)
    
    wind.mainloop()

eee,有点长。

不过有注释了。

希望大家能理解。😅😅😅

2.3查询:

查账和记账差不多

所以 我就偷了点懒复制了一下

区别如下:

  1. 查询不要求每一栏都输入。
  2. 所以日期不能默认。

做出来的效果和输入差不多,具体如下:

 

具体情况和输入差不多,故不做讲解

代码如下:

def look_f():
    l_f = Toplevel(root)
    l_f.title("查询")
    l_f.geometry("300x200")
    l_f.resizable(width = False, height = False)

    def clo(): #关闭窗口
        l_f.destroy()

    def find(s): #查找完成时的提示语
        if s == "":
            messagebox.showinfo("提示", "没有找到你要的内容。")
        else:
            give = Toplevel(l_f)
            give.title("结果")
            def close():
                give.destroy()
            need = Message(give, text = s, width = 230)
            need.grid()
            x = Button(give, text = "确定", command = close)
            x.grid(row = 2)

    def qd(): #查找符合条件的账目
        global w1, w, kind
        right = ""
        wh = True
        m = True
        tim = True
        
        w1 = str(g.get())
        if w1 == "":
            wh = False
        cost = str(c_g.get())
        if cost == "":
            m = False
        t1 = str(t_g.get())
        if t1 == "":
            tim = False
        else:
            t = ""
            for i in t1:
                if i >= '0' and i <= '9':
                    t += i
                else:
                    t += " "
            t1 = t
        if kind == "支出":
            cost = '-' + cost
        
        pf = open("家庭账本", "br")
        ls = load(pf)
        pf.close()

        while ls != [["日期", "事项", "金额"]]:
            flag = True
            temp = ls.pop()
            if (tim and temp[0] != t1):
                flag = False
            if (wh and temp[1][1] != w1):
                flag = False
            if w != temp[1][0]:
                flag = False
            if (m and temp[2] != cost):
                flag = False
            if flag:
                right += temp[0] + '    ' + temp[1][0] + '-' + temp[1][1] + '    ' + temp[2] + '\n'
        
        g.delete(0, "end")
        c_g.delete(0, "end")
        t_g.delete(0, "end")
        w1 = ""
        find(right)
    
    def do(what): #设置事项
        global w
        w = what
        out = Label(l_f, text = "事项: " + w)
        out.grid(row = 0, column = 0)
        if w in "饮食娱乐购物学习其他":
            do_k("支出")
        elif w == "其他 ":
            do_k("收入")
        else:
            do_k("收入")

    def do_k(what): #设置操作
        global kind
        kind = what
        out_k = Label(l_f, text = "操作类型: " + kind)
        out_k.grid(row = 0, column = 1)
    
    do("其他")
    do_k("支出")
    
    #菜单条
    menubar = Menu(l_f)
    l_f.config(menu=menubar)

    #菜单
    m = Menu(l_f)
    for i in ["饮食", "娱乐", "购物", "学习", "其他"]:
        if i == "其他":
            m.add_separator() #添加分割线
        do1 = partial(do, i)
        m.add_command(label = i, command = do1)
    menubar.add_cascade(label = "支出", menu = m)
    out = Menu(l_f)
    for i in ["工资", "福利", "理财", "其他 "]:
        if i == "其他 ":
            out.add_separator() #添加分割线
        do1 = partial(do, i)
        out.add_command(label = i, command = do1)
    menubar.add_cascade(label = "收入", menu = out)
    
    l = Label(l_f, text = "请输入具体事项:", bg = "light sky blue", fg = "white")#提示语
    l.grid(row = 2, column = 0)#行、列、位置(NSWE)
    g = Entry(l_f) #文本框
    g.grid(row = 2, column = 1)

    c_l = Label(l_f, text = "请输入金额:", bg = "sky blue", fg = "white")
    c_l.grid(row = 3, column = 0)
    c_g = Entry(l_f)
    c_g.grid(row = 3, column = 1)

    t_l = Label(l_f, text = "请输入时间:", bg = "deep sky blue", fg = "white")
    t_l.grid(row = 4, column = 0)
    t_g = Entry(l_f)
    t_g.grid(row = 4, column = 1)

    ok = Button(l_f, text = "确定", command = qd)
    close = Button(l_f, text = "取消", command = clo)
    temp = Label(l_f, text = "  ")
    temp.grid(row = 5)
    ok.grid(row = 6, column = 0)
    close.grid(row = 6, column = 1)

    l_f.mainloop()

还是有点多啊。不过如果你已经看懂了输入理解这个也就不难了。

2.4统计:

这个就相对简单一些了。

(搞了半天的难度,也该做点简单的放松一下了。)

目标如下:

  • 查询一段时间内的消费记录,并生成报告。

报告包括以下内容:

  1. 分开统计收入与支出。
  2. 两者都包括:总金额,各事项的金额,各事项金额占总金额的百分比。

相信懂点编程的都看得出来,这就是一个简单的统计。

注意!!!注意!!!注意!!!

此处输入的日期格式为:

        四位数表示年份,两位数表示月份,两位数表示日期,中间用一个分隔符(空格,逗号,句号等)具体如下:

 生成的报告如下:

 代码如下:

def know():
    show = Toplevel(root)
    show.title("统计")
    show.resizable(width = False, height = False)

    
    f = Label(show, text = "请输入开始时间:")
    f.grid(row = 0, column = 0)
    g_f = Entry(show)
    g_f.grid(row = 0, column = 1)
    t = Label(show, text = "请输入结束时间:")
    t.grid(row = 1, column = 0)
    g_t = Entry(show)
    g_t.grid(row = 1, column = 1)
    
    def g():       
        fro = ""
        to = ""
        s_i = 0
        s_o = 0
        d = {}
        
        fro = g_f.get()
        to = g_t.get()
        if fro == "":
            messagebox.showinfo("提示", "请输入开始时间")
            return
        if to == "":
            messagebox.showinfo("提示", "请输入结束时间")
            return
        g_f.delete(0, "end")
        g_t.delete(0, 'end')
        
        pf = open("家庭账本", "br")
        ls = load(pf)
        pf.close()
        ls.pop(0)
        
        for i in ls:
            flag = True
            for j in range(10):
                if not(i[0][j] >= fro[j] and i[0][j] <= to[j]):
                    flag = False
            if flag:
                if int(i[2]) < 0:
                    s_o += abs(int(i[2]))
                    if d.get(i[1][0]+"消费", 0):
                        d[i[1][0]+"消费"] += abs(int(i[2]))
                    else:
                        d[i[1][0]+"消费"] = abs(int(i[2]))                  
                else:
                   s_i += int(i[2])
                   if d.get(i[1][0], 0):
                       d[i[1][0]] += int(i[2])
                   else:
                       d[i[1][0]] = int(i[2])
        ans = Toplevel(show)
        ans.title("结果")

        m = Message(ans, text = "这段时间总消费" + str(abs(s_o)) + "元,其中:\n" +
                    "饮食消费共" + str(abs(d.get("饮食消费", 0))) + "元,约占总支出的" + str(round(abs(d.get("饮食消费", 0)) / max(s_o, 1) * 100, 2)) + "%。\n" +
                    "娱乐消费共" + str(abs(d.get("娱乐消费", 0))) + "元,约占总支出的" + str(round(abs(d.get("娱乐消费", 0)) / max(s_o, 1) * 100, 2)) + "%。\n" +
                    "学习消费共" + str(abs(d.get("学习消费", 0))) + "元,约占总支出的" + str(round(abs(d.get("学习消费", 0)) / max(s_o, 1) * 100, 2)) + "%。\n" +
                    "购物消费共" + str(abs(d.get("购物消费", 0))) + "元,约占总支出的" + str(round(abs(d.get("购物消费", 0)) / max(s_o, 1) * 100, 2)) + "%。\n" +
                    "其他消费共" + str(abs(d.get("其他消费", 0))) + "元,约占总支出的" + str(round(abs(d.get("其他消费", 0)) / max(s_o, 1) * 100, 2)) + "%。\n" +
                    "总收入" + str(s_i) + "元,其中:\n" +
                    "工资收入共" + str(d.get("工资", 0)) + "元,约占总收入的" + str(round(d.get("工资", 0) / max(s_i, 1) * 100, 2)) + "%。\n" +
                    "福利收入共" + str(d.get("福利", 0)) + "元,约占总收入的" + str(round(d.get("福利", 0) / max(s_i, 1) * 100, 2)) + "%。\n" +
                    "理财收入共" + str(d.get("理财", 0)) + "元,约占总收入的" + str(round(d.get("理财", 0) / max(s_i, 1) * 100, 2)) + "%。\n" +
                    "其他收入共" + str(d.get("其他", 0)) + "元,约占总收入的" + str(round(d.get("其他", 0) / max(s_i, 1) * 100, 2)) + "%。\n")
        m.grid()
        
        ans.mainloop()
        return

    def clo():
        show.destroy()
    
    b = Button(show, text = "确定", command = g)
    b.grid(row = 2)
    c = Button(show, text = "关闭", command = clo)
    c.grid(row = 2, column = 1)
    show.mainloop()

大概就是这样。

3.结尾:

啊啊啊终于做好了。

3.1 总代码:
from tkinter import * #构建页面
from tkinter import messagebox
from functools import * #包装函数
from time import * #获取时间
from pickle import * #永久保存


root = Tk() #生成窗口
root.title("家庭账本") #标题
root.geometry("500x500") #大小
root.resizable(width = False, height = False) #不可改变大小

w = "" #事项
w1 = "" #具体事项
kind = "支出" #存入||指出
cost = 0
t = ""
ls = []

def click():
    wind = Toplevel(root) #子窗口
    wind.title("记账")
    wind.geometry("300x200")
    wind.resizable(width = False, height = False)

    def without(s): #保存时的提示语
        done = Toplevel(wind)
        if s == None:
            temp = Label(done, text = "保存完毕,请继续")
            temp.grid(row = 0)
        else:
            temp = Label(done, text = "请填写" + s)
            temp.grid(row = 0)
        did = partial(done.destroy)
        but = Button(done, text = "确定", command = did)
        but.grid(row = 1)

    def qd(): #保存记账,并继续
        global w1, w, kind
        w1 = str(g.get())
        if w1 == "":
            without("事项")
            return
        wh = [w, w1]
        cost = str(c_g.get())
        if cost == "":
            without("金额")
            return
        t1 = str(t_g.get())
        if t1 != "":
            t = ""
            for i in t1:
                if i >= '0' and i <= '9':
                    t += i
                else:
                    t += " "
        else:
            t = strftime("%Y %m %d", localtime())
        if kind == "支出":
            cost = '-' + cost
        pf = open("家庭账本", "br")
        ls = load(pf)
        pf.close()
        ls.append([t, wh, cost])
        pf = open("家庭账本", "bw")
        dump(ls, pf)
        pf.close()
        g.delete(0, "end")
        c_g.delete(0, "end")
        t_g.delete(0, "end")
        w1 = ""
        without(None)

    def clo(): #关闭窗口
        wind.destroy()
    
    def do(what): #设置事项
        global w
        w = what
        out = Label(wind, text = "事项: " + w)
        out.grid(row = 0, column = 0)
        if w in "饮食娱乐购物学习其他":
            do_k("支出")
        elif w == "其他 ":
            do_k("收入")
        else:
            do_k("收入")

    def do_k(what): #设置操作
        global kind
        kind = what
        out_k = Label(wind, text = "操作类型: " + kind)
        out_k.grid(row = 0, column = 1)

    def do_t(): #设置时间
        out_t = Label(wind, text = "默认时间: " + strftime("%Y %m %d", localtime()))
        out_t.grid(row = 1, column = 0)
    
    do("其他")
    do_k("支出")
    do_t()
    
    #菜单条
    menubar = Menu(wind)
    wind.config(menu=menubar)

    #菜单
    m = Menu(wind)
    for i in ["饮食", "娱乐", "购物", "学习", "其他"]:
        if i == "其他":
            m.add_separator() #添加分割线
        do1 = partial(do, i)
        m.add_command(label = i, command = do1)
    menubar.add_cascade(label = "支出", menu = m)

    out = Menu(wind)
    for i in ["工资", "福利", "理财", "其他 "]:
        if i == "其他 ":
            out.add_separator() #添加分割线
        do1 = partial(do, i)
        out.add_command(label = i, command = do1)
    menubar.add_cascade(label = "收入", menu = out)
    
    l = Label(wind, text = "请输入具体事项:", bg = "light sky blue", fg = "white")#提示语
    l.grid(row = 2, column = 0)#行、列、位置(NSWE)
    g = Entry(wind) #文本框
    g.grid(row = 2, column = 1)

    c_l = Label(wind, text = "请输入金额:", bg = "sky blue", fg = "white")
    c_l.grid(row = 3, column = 0)
    c_g = Entry(wind)
    c_g.grid(row = 3, column = 1)

    t_l = Label(wind, text = "请输入时间:", bg = "deep sky blue", fg = "white")
    t_l.grid(row = 4, column = 0)
    t_g = Entry(wind)
    t_g.grid(row = 4, column = 1)

    ok = Button(wind, text = "确定", command = qd)
    close = Button(wind, text = "取消", command = clo)
    temp = Label(wind, text = "  ")
    temp.grid(row = 5)
    ok.grid(row = 6, column = 0)
    close.grid(row = 6, column = 1)
    
    wind.mainloop()

def look_f():
    l_f = Toplevel(root)
    l_f.title("查询")
    l_f.geometry("300x200")
    l_f.resizable(width = False, height = False)

    def clo(): #关闭窗口
        l_f.destroy()

    def find(s): #查找完成时的提示语
        if s == "":
            messagebox.showinfo("提示", "没有找到你要的内容。")
        else:
            give = Toplevel(l_f)
            give.title("结果")
            def close():
                give.destroy()
            need = Message(give, text = s, width = 230)
            need.grid()
            x = Button(give, text = "确定", command = close)
            x.grid(row = 2)

    def qd(): #查找符合条件的账目
        global w1, w, kind
        right = ""
        wh = True
        m = True
        tim = True
        
        w1 = str(g.get())
        if w1 == "":
            wh = False
        cost = str(c_g.get())
        if cost == "":
            m = False
        t1 = str(t_g.get())
        if t1 == "":
            tim = False
        else:
            t = ""
            for i in t1:
                if i >= '0' and i <= '9':
                    t += i
                else:
                    t += " "
            t1 = t
        if kind == "支出":
            cost = '-' + cost
        
        pf = open("家庭账本", "br")
        ls = load(pf)
        pf.close()

        while ls != [["日期", "事项", "金额"]]:
            flag = True
            temp = ls.pop()
            if (tim and temp[0] != t1):
                flag = False
            if (wh and temp[1][1] != w1):
                flag = False
            if w != temp[1][0]:
                flag = False
            if (m and temp[2] != cost):
                flag = False
            if flag:
                right += temp[0] + '    ' + temp[1][0] + '-' + temp[1][1] + '    ' + temp[2] + '\n'
        
        g.delete(0, "end")
        c_g.delete(0, "end")
        t_g.delete(0, "end")
        w1 = ""
        find(right)
    
    def do(what): #设置事项
        global w
        w = what
        out = Label(l_f, text = "事项: " + w)
        out.grid(row = 0, column = 0)
        if w in "饮食娱乐购物学习其他":
            do_k("支出")
        elif w == "其他 ":
            do_k("收入")
        else:
            do_k("收入")

    def do_k(what): #设置操作
        global kind
        kind = what
        out_k = Label(l_f, text = "操作类型: " + kind)
        out_k.grid(row = 0, column = 1)
    
    do("其他")
    do_k("支出")
    
    #菜单条
    menubar = Menu(l_f)
    l_f.config(menu=menubar)

    #菜单
    m = Menu(l_f)
    for i in ["饮食", "娱乐", "购物", "学习", "其他"]:
        if i == "其他":
            m.add_separator() #添加分割线
        do1 = partial(do, i)
        m.add_command(label = i, command = do1)
    menubar.add_cascade(label = "支出", menu = m)
    out = Menu(l_f)
    for i in ["工资", "福利", "理财", "其他 "]:
        if i == "其他 ":
            out.add_separator() #添加分割线
        do1 = partial(do, i)
        out.add_command(label = i, command = do1)
    menubar.add_cascade(label = "收入", menu = out)
    
    l = Label(l_f, text = "请输入具体事项:", bg = "light sky blue", fg = "white")#提示语
    l.grid(row = 2, column = 0)#行、列、位置(NSWE)
    g = Entry(l_f) #文本框
    g.grid(row = 2, column = 1)

    c_l = Label(l_f, text = "请输入金额:", bg = "sky blue", fg = "white")
    c_l.grid(row = 3, column = 0)
    c_g = Entry(l_f)
    c_g.grid(row = 3, column = 1)

    t_l = Label(l_f, text = "请输入时间:", bg = "deep sky blue", fg = "white")
    t_l.grid(row = 4, column = 0)
    t_g = Entry(l_f)
    t_g.grid(row = 4, column = 1)

    ok = Button(l_f, text = "确定", command = qd)
    close = Button(l_f, text = "取消", command = clo)
    temp = Label(l_f, text = "  ")
    temp.grid(row = 5)
    ok.grid(row = 6, column = 0)
    close.grid(row = 6, column = 1)

    l_f.mainloop()

def know():
    show = Toplevel(root)
    show.title("统计")
    show.resizable(width = False, height = False)

    
    f = Label(show, text = "请输入开始时间:")
    f.grid(row = 0, column = 0)
    g_f = Entry(show)
    g_f.grid(row = 0, column = 1)
    t = Label(show, text = "请输入结束时间:")
    t.grid(row = 1, column = 0)
    g_t = Entry(show)
    g_t.grid(row = 1, column = 1)
    
    def g():       
        fro = ""
        to = ""
        s_i = 0
        s_o = 0
        d = {}
        
        fro = g_f.get()
        to = g_t.get()
        if fro == "":
            messagebox.showinfo("提示", "请输入开始时间")
            return
        if to == "":
            messagebox.showinfo("提示", "请输入结束时间")
            return
        g_f.delete(0, "end")
        g_t.delete(0, 'end')
        
        pf = open("家庭账本", "br")
        ls = load(pf)
        pf.close()
        ls.pop(0)
        
        for i in ls:
            flag = True
            for j in range(10):
                if not(i[0][j] >= fro[j] and i[0][j] <= to[j]):
                    flag = False
            if flag:
                if int(i[2]) < 0:
                    s_o += abs(int(i[2]))
                    if d.get(i[1][0]+"消费", 0):
                        d[i[1][0]+"消费"] += abs(int(i[2]))
                    else:
                        d[i[1][0]+"消费"] = abs(int(i[2]))                  
                else:
                   s_i += int(i[2])
                   if d.get(i[1][0], 0):
                       d[i[1][0]] += int(i[2])
                   else:
                       d[i[1][0]] = int(i[2])
        ans = Toplevel(show)
        ans.title("结果")

        m = Message(ans, text = "这段时间总消费" + str(abs(s_o)) + "元,其中:\n" +
                    "饮食消费共" + str(abs(d.get("饮食消费", 0))) + "元,约占总支出的" + str(round(abs(d.get("饮食消费", 0)) / max(s_o, 1) * 100, 2)) + "%。\n" +
                    "娱乐消费共" + str(abs(d.get("娱乐消费", 0))) + "元,约占总支出的" + str(round(abs(d.get("娱乐消费", 0)) / max(s_o, 1) * 100, 2)) + "%。\n" +
                    "学习消费共" + str(abs(d.get("学习消费", 0))) + "元,约占总支出的" + str(round(abs(d.get("学习消费", 0)) / max(s_o, 1) * 100, 2)) + "%。\n" +
                    "购物消费共" + str(abs(d.get("购物消费", 0))) + "元,约占总支出的" + str(round(abs(d.get("购物消费", 0)) / max(s_o, 1) * 100, 2)) + "%。\n" +
                    "其他消费共" + str(abs(d.get("其他消费", 0))) + "元,约占总支出的" + str(round(abs(d.get("其他消费", 0)) / max(s_o, 1) * 100, 2)) + "%。\n" +
                    "总收入" + str(s_i) + "元,其中:\n" +
                    "工资收入共" + str(d.get("工资", 0)) + "元,约占总收入的" + str(round(d.get("工资", 0) / max(s_i, 1) * 100, 2)) + "%。\n" +
                    "福利收入共" + str(d.get("福利", 0)) + "元,约占总收入的" + str(round(d.get("福利", 0) / max(s_i, 1) * 100, 2)) + "%。\n" +
                    "理财收入共" + str(d.get("理财", 0)) + "元,约占总收入的" + str(round(d.get("理财", 0) / max(s_i, 1) * 100, 2)) + "%。\n" +
                    "其他收入共" + str(d.get("其他", 0)) + "元,约占总收入的" + str(round(d.get("其他", 0) / max(s_i, 1) * 100, 2)) + "%。\n")
        m.grid()
        
        ans.mainloop()
        return

    def clo():
        show.destroy()
    
    b = Button(show, text = "确定", command = g)
    b.grid(row = 2)
    c = Button(show, text = "关闭", command = clo)
    c.grid(row = 2, column = 1)
    show.mainloop()

wri = Button(root, text = '记账', command = click) #设置按钮
wri.grid(row = 0, column = 0)
rea = Button(root, text = '查询', command = look_f) #查询键
rea.grid(row = 0, column = 1)
get = Button(root, text = '生成报告', command = know) #统计键
get.grid(row = 0, column = 2)

can = Canvas(root, width = 430, height = 450)
pla = PhotoImage(file = "存钱罐.gif")
pic = can.create_image(250, 250, image=pla)
can.grid(row = 1, columnspan = 3)

root.mainloop()


啊!真长!!!

我被我自己震惊了。

3.2前导代码以及其他前导内容:

注意!!!你现在运行这段代码依然有bug!!!

有两样东西还没有!!!

3.2.1前导代码:

因为Python会一开始就文件读入"家庭账本",然鹅没有该文件,故需添加前导代码一段。

代码:

from pickle import * 
ls = [['日期', '事项', '金额']]
pf = open("家庭账本", "bw")
dump(ls, pf)
pf.close()

讲解一下:

本代码会将“家庭账本”这个文件保存至当前路径,并初始化。

使用方法:

将其与你的家庭账本放置于同一路径下,运行一次即可。

3.2.2前导图片:

需要有一张名为“存钱罐”的.gif格式图片。这里提供我使用的图片:

 注:若图片大小与上图相差太大,建议更改之。

3.3大结局:

终于做完了,累死了。

本人对Tkinter使用还不是很熟练,如有可优化之处,欢迎指出。

欢迎点赞、评论、关注。

期待有大佬提出优化方案。

谢谢。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/798445.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-06
下一篇 2022-05-06

发表评论

登录后才能评论

评论列表(0条)

保存