Держите.
Но много неудачных моментов по ООП для tkinter. Но начало положено, а это гуд.
#
from tkinter import *
from tkinter import ttk
import sqlite3
conn = sqlite3.connect('finance.db')
c = conn.cursor()
c.execute("""CREATE TABLE IF NOT EXISTS finance (id integer primary key, description text, costs text, total real, date current_timestamp)""")
c.execute('''select * from finance''')
class Main:
def __init__(self, master):
self.master = master
self.master.title('Домашние финансы')
self.master.geometry('650x450+300+200')
self.master.resizable(False, False)
toolbar = Frame(master)
toolbar.pack(side=TOP, fill=X)
toolbar.config(bg='#d7d8e0', bd=2)
self.add_img = PhotoImage(file="add.gif")
self.btnOpenDialog = Button(toolbar, text='Добавить позицию', command=self.openDialog, image=self.add_img)
self.btnOpenDialog.pack(side=LEFT)
self.btnOpenDialog.config(bg='#d7d8e0', bd=0, compound=TOP)
self.tree = ttk.Treeview(self.master, columns=('ID', 'description', 'costs', 'total', 'date'),
height=15, show='headings')
self.tree.column("ID", width=30, anchor=CENTER)
self.tree.column("description", width=250, anchor=CENTER)
self.tree.column("costs", width=150, anchor=CENTER)
self.tree.column("total", width=100, anchor=CENTER)
self.tree.column("date", width=100, anchor=CENTER)
self.tree.heading("ID", text='ID')
self.tree.heading("description", text='Наименование')
self.tree.heading("costs", text='Статья дохода/расхода')
self.tree.heading("total", text='Сумма')
self.tree.heading("date", text='Дата')
for row in c.fetchall():
self.tree.insert('', 'end', values=row)
self.tree.yview()
self.tree.pack(side=TOP, fill=X)
self.master.mainloop()
def add_data(self, data):
""""""
print(data)
# insert here
self.tree.insert("")
def open_dialog(self):
""""""
# inject main to child
Child(self.master, main=self)
class Child:
def __init__(self, master, main):
self.main = main
self.slave = Toplevel(master)
self.slave.title('Добавить доходы/расходы')
self.slave.geometry('400x220+400+300')
self.slave.resizable(False, False)
self.labelDescription = Label(self.slave, text='Наименование:')
self.labelDescription.place(x=50, y=50)
self.labelSelect = Label(self.slave, text='Статья дохода/расхода:')
self.labelSelect.place(x=50, y=80)
self.labelSum = Label(self.slave, text='Сумма:')
self.labelSum.place(x=50, y=110)
self.entryDescription = ttk.Entry(self.slave)
self.entryDescription.place(x=200, y=50)
self.entryMoney = ttk.Entry(self.slave)
self.entryMoney.place(x=200, y=110)
self.combobox = ttk.Combobox(self.slave, values=[u"Доход", u"Расход"])
self.combobox.current(0)
self.combobox.place(x=200, y=80)
self.btnCancel = ttk.Button(self.slave, text='Отмена', command=self.slave.destroy)
self.btnCancel.place(x=300, y=170)
self.btnOk = ttk.Button(self.slave, text='OK')
self.btnOk.place(x=220, y=170)
self.btnOk.bind('<Button-1>', lambda event: self.insertData(str(self.entryDescription.get()),
str(self.combobox.get()),
float(self.entryMoney.get())))
self.slave.grab_set()
self.slave.focus_set()
self.slave.wait_window()
def insert_data(self, description, costs, total):
c.execute('''INSERT INTO finance(description, costs, sum) VALUES (?, ?, ?)''', (description, costs, total))
conn.commit()
'''conn.close()'''
# call main.add_data
self.main.add_data((description, costs, total))
self.slave.destroy()
root = Tk()
Main(root)