Python 3 Tkinter Expense Money or Budget Household Finance Tracker GUI Desktop App

Python 3 Tkinter Expense Money or Budget Household Finance Tracker GUI Desktop App

Python 3 Tkinter Expense Money or Budget Household Finance Tracker GUI Desktop App

# -*- coding:utf-8 -*-
import tkinter as tk
from tkinter import ttk
import sqlite3

class Main(tk.Frame):
    def __init__(self, root):
        self.db = db

    def init_main(self):
        toolbar = tk.Frame(bg='#d7d8e0', bd=2)
        toolbar.pack(side=tk.TOP, fill=tk.X)

        self.add_image = tk.PhotoImage(file='images/for-sale_48.png')
        btn_open_dialog = tk.Button(toolbar, text='Добавить', command=self.open_dialog, bg='#d7d8e0', bd=0, compound=tk.TOP, image=self.add_image)

        self.tree = ttk.Treeview(self, columns=('ID', 'description', 'costs', 'total'), height=15, show='headings')

        self.tree.column('ID', width=30, anchor=tk.CENTER)
        self.tree.column('description', width=365, anchor=tk.CENTER)
        self.tree.column('costs', width=150, anchor=tk.CENTER)
        self.tree.column('total', width=100, anchor=tk.CENTER)

        self.tree.heading('ID', text='ID')
        self.tree.heading('description', text='Наименование')
        self.tree.heading('costs', text='Статья дохода/расхода')
        self.tree.heading('total', text='Сумма')

    def records(self, description, costs, total):
        self.db.insert_data(description, costs, total)

    def view_records(self):
        self.db.c.execute('''SELECT * FROM finance''')

[self.tree.delete(i) for i in self.tree.get_children()]

[self.tree.insert(”, ‘end’, values=row) for row in self.db.c.fetchall()]

def open_dialog(self): Child() class Child(tk.Toplevel): def __init__(self): super().__init__(root) self.init_child() self.view = app def init_child(self): self.title(‘Добавить доходырасходы’) self.geometry(‘400×220+400+300′) self.resizable(False, False) label_description = tk.Label(self, text=’Наименование’), y=50) label_select = tk.Label(self, text=’Статья дохода/расхода’), y=80) label_sum = tk.Label(self, text=’Сумма’), y=110) self.entry_description = ttk.Entry(self), y=50) self.entry_money = ttk.Entry(self), y=110) self.combobox = ttk.Combobox(self, values=[u’Доход’, u’Расход’]) self.combobox.current(0), y=80) btn_cancel = ttk.Button(self, text=’Закрыть’, command=self.destroy), y=170) btn_ok = ttk.Button(self, text=’Добавить’), y=170) btn_ok.bind(‘<Button-1>’, lambda event: self.view.records(self.entry_description.get(), self.entry_money.get(), self.combobox.get())) self.grab_set() self.focus_set() class DB: def __init__(self): self.conn = sqlite3.connect(‘finance.db’) self.c = self.conn.cursor() self.c.execute(”’CREATE TABLE IF NOT EXISTS finance (id integer primary key, description text, costs text, total real)”’) self.conn.commit() def insert_data(self, description, costs, total): self.c.execute(”’INSERT INTO finance(description, costs, total) VALUES (?, ?, ?)”’, (description, costs, total)) self.conn.commit() if __name__ == “__main__”: root = tk.Tk() db = DB() app = Main(root) app.pack() root.title(‘Household finance’) root.geometry(‘650×450+300+200’) root.resizable(False, False) root.mainloop()

Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment