· 6 years ago · Apr 15, 2019, 10:42 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.update_img = tk.PhotoImage(file='update.gif')
23 btn_edit_dialog = tk.Button(toolbar, text='Редактировать', bg='#d7d8e0', bd=0, image=self.update_img,
24 compound=tk.TOP, command=self.open_update_dialog)
25 btn_edit_dialog.pack(side=tk.LEFT)
26
27 self.tree = ttk.Treeview(self, columns=('ID', 'description', 'costs', 'total'), height=15, show='headings')
28
29 self.tree.column('ID', width=30, anchor=tk.CENTER)
30 self.tree.column('description', width=365, anchor=tk.CENTER)
31 self.tree.column('costs', width=150, anchor=tk.CENTER)
32 self.tree.column('total', width=100, anchor=tk.CENTER)
33
34 self.tree.heading('ID', text='ID')
35 self.tree.heading('description', text='Ðаименование')
36 self.tree.heading('costs', text='Ð¡Ñ‚Ð°Ñ‚ÑŒÑ Ð´Ð¾Ñ…Ð¾Ð´Ð°/раÑхода')
37 self.tree.heading('total', text='Сумма')
38
39 self.tree.pack()
40
41 def records(self, description, costs, total):
42 self.db.insert_data(description, costs, total)
43 self.view_records()
44
45 def update_record(self, description, costs, total):
46 self.db.c.execute('''UPDATE finance SET description=?, costs=?, total=? WHERE ID=?''',
47 (description, costs, total, self.tree.set(self.tree.selection()[0], '#1')))
48 self.db.conn.commit()
49 self.view_records()
50
51 def view_records(self):
52 self.db.c.execute('''SELECT * FROM finance''')
53 [self.tree.delete(i) for i in self.tree.get_children()]
54 [self.tree.insert('', 'end', values=row) for row in self.db.c.fetchall()]
55
56 def open_dialog(self):
57 Child()
58
59 def open_update_dialog(self):
60 Update()
61
62
63class Child(tk.Toplevel):
64 def __init__(self):
65 super().__init__(root)
66 self.init_child()
67 self.view = app
68
69 def init_child(self):
70 self.title('Добавить доходы/раÑходы')
71 self.geometry('400x220+400+300')
72 self.resizable(False, False)
73
74 label_description = tk.Label(self, text='Ðаименование:')
75 label_description.place(x=50, y=50)
76 label_select = tk.Label(self, text='Ð¡Ñ‚Ð°Ñ‚ÑŒÑ Ð´Ð¾Ñ…Ð¾Ð´Ð°/раÑхода:')
77 label_select.place(x=50, y=80)
78 label_sum = tk.Label(self, text='Сумма:')
79 label_sum.place(x=50, y=110)
80
81 self.entry_description = ttk.Entry(self)
82 self.entry_description.place(x=200, y=50)
83
84 self.entry_money = ttk.Entry(self)
85 self.entry_money.place(x=200, y=110)
86
87 self.combobox = ttk.Combobox(self, values=[u'Доход', u'РаÑход'])
88 self.combobox.current(0)
89 self.combobox.place(x=200, y=80)
90
91 btn_cancel = ttk.Button(self, text='Закрыть', command=self.destroy)
92 btn_cancel.place(x=300, y=170)
93
94 self.btn_ok = ttk.Button(self, text='Добавить')
95 self.btn_ok.place(x=220, y=170)
96 self.btn_ok.bind('<Button-1>', lambda event: self.view.records(self.entry_description.get(),
97 self.combobox.get(),
98 self.entry_money.get()))
99
100 self.grab_set()
101 self.focus_set()
102
103
104class Update(Child):
105 def __init__(self):
106 super().__init__()
107 self.init_edit()
108 self.view = app
109
110 def init_edit(self):
111 self.title('Редактировать позицию')
112 btn_edit = ttk.Button(self, text='Редактировать')
113 btn_edit.place(x=205, y=170)
114 btn_edit.bind('<Button-1>', lambda event: self.view.update_record(self.entry_description.get(),
115 self.combobox.get(),
116 self.entry_money.get()))
117
118 self.btn_ok.destroy()
119
120
121class DB:
122 def __init__(self):
123 self.conn = sqlite3.connect('finance.db')
124 self.c = self.conn.cursor()
125 self.c.execute(
126 '''CREATE TABLE IF NOT EXISTS finance (id integer primary key, description text, costs text, total real)''')
127 self.conn.commit()
128
129 def insert_data(self, description, costs, total):
130 self.c.execute('''INSERT INTO finance(description, costs, total) VALUES (?, ?, ?)''',
131 (description, costs, total))
132 self.conn.commit()
133
134
135if __name__ == "__main__":
136 root = tk.Tk()
137 db = DB()
138 app = Main(root)
139 app.pack()
140 root.title("Household finance")
141 root.geometry("650x450+300+200")
142 root.resizable(False, False)
143 root.mainloop()