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