· 7 years ago · Jan 06, 2019, 08:32 PM
1import tkinter as tk
2from tkinter import ttk
3import sqlite3
4
5
6class Main(tk.Frame):
7 def __init__(self, root):
8 super().__init__(root)
9 self.init_main()
10 self.db = db
11 self.view_records()
12
13 def init_main(self):
14 toolbar = tk.Frame(bg='#d7d8e0', bd=2)
15 toolbar.pack(side=tk.TOP, fill=tk.X)
16
17 self.add_img = tk.PhotoImage(file='add.gif')
18 btn_open_dialog = tk.Button(toolbar, text='Добавить позицию', command=self.open_dialog, bg='#d7d8e0', bd=0,
19 compound=tk.TOP, image=self.add_img)
20 btn_open_dialog.pack(side=tk.LEFT)
21
22 self.tree = ttk.Treeview(self, columns=('ID', 'description', 'costs', 'total'), height=15, show='headings')
23
24 self.tree.column('ID', width=30, anchor=tk.CENTER)
25 self.tree.column('description', width=365, anchor=tk.CENTER)
26 self.tree.column('costs', width=150, anchor=tk.CENTER)
27 self.tree.column('total', width=100, anchor=tk.CENTER)
28
29 self.tree.heading('ID', text='ID')
30 self.tree.heading('description', text='Ðаименование')
31 self.tree.heading('costs', text='Ð¡Ñ‚Ð°Ñ‚ÑŒÑ Ð´Ð¾Ñ…Ð¾Ð´Ð°\раÑхода')
32 self.tree.heading('total', text='Сумма')
33
34 self.tree.pack()
35
36 def records(self, description, costs, total):
37 self.db.insert_data(description, costs, total)
38 self.view_records()
39
40 def view_records(self):
41 self.db.c.execute('''SELECT * FROM finance''')
42 [self.tree.delete(i) for i in self.tree.get_children()]
43 [self.tree.insert('', 'end', values=row) for row in self.db.c.fetchall()]
44
45 def open_dialog(self):
46 Child()
47
48
49class Child(tk.Toplevel):
50 def __init__(self):
51 super().__init__(root)
52 self.init_child()
53 self.view = app
54
55 def init_child(self):
56 self.title('Добавить доходы\раÑходы')
57 self.geometry('400x220+400+300')
58 self.resizable(False, False)
59
60 label_description = tk.Label(self, text='Ðаименование:')
61 label_description.place(x=50, y=50)
62 label_select = tk.Label(self, text='Ð¡Ñ‚Ð°Ñ‚ÑŒÑ Ð´Ð¾Ñ…Ð¾Ð´Ð°\раÑхода:')
63 label_select.place(x=50, y=80)
64 label_sum = tk.Label(self, text='Сумма:')
65 label_sum.place(x=50, y=110)
66
67 self.entry_description = ttk.Entry(self)
68 self.entry_description.place(x=200, y=50)
69
70 self.entry_money = ttk.Entry(self)
71 self.entry_money.place(x=200, y=110)
72
73 self.combobox = ttk.Combobox(self, values=[u'Доход', u'РаÑход'])
74 self.combobox.current(0)
75 self.combobox.place(x=200, y=80)
76
77 btn_cancel = ttk.Button(self, text='Закрыть', command=self.destroy)
78 btn_cancel.place(x=300, y=170)
79
80 btn_ok = ttk.Button(self, text='Добавить')
81 btn_ok.place(x=220, y=170)
82 btn_ok.bind('<Button-1>', lambda event: self.view.records(self.entry_description.get(),
83 self.entry_money.get(),
84 self.combobox.get()))
85
86 self.grab_set()
87 self.focus_set()
88
89
90class DB:
91 def __init__(self):
92 self.conn = sqlite3.connect('finance.db')
93 self.c = self.conn.cursor()
94 self.c.execute(
95 '''CREATE TABLE IF NOT EXISTS finance (id integer primary key, description text, costs text, total real)''')
96 self.conn.commit()
97
98 def insert_data(self, description, costs, total):
99 self.c.execute('''INSERT INTO finance(description, costs, total) VALUES (?, ?, ?)''',
100 (description, costs, total))
101 self.conn.commit()
102
103
104if __name__ == "__main__":
105 root = tk.Tk()
106 db = DB()
107 app = Main(root)
108 app.pack()
109 root.title("Household finance")
110 root.geometry("650x450+300+200")
111 root.resizable(False, False)
112 root.mainloop()