-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
186 lines (151 loc) · 6.56 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import tkinter as tk
from tkinter import ttk
import os
from user import *
from db_functions import *
from VKeyboard import VKeyboard
from ttkbootstrap import Style
class App(tk.Tk):
path_to_file = os.path.abspath(__file__)
dir_path = os.path.dirname(path_to_file)
database = dir_path+"/database/kittybase.sqlite3"
def __init__(self):
super().__init__()
# Initiate virtual keyboard
# VKeyboard(self)
# Configure the toplevel
self.title("Cafe App")
self.attributes("-fullscreen", True)
# Configure label style
style = Style()
style.configure('TLabel', font=('Helvetica', 30))
# Head frame
self.frame_header = ttk.Frame()
self.frame_header.pack(pady=0)
self.logo = tk.PhotoImage(file=self.dir_path+'/img/cafe_logo.png')
self.header_logo = ttk.Label(self.frame_header,
image=self.logo)
self.header_logo.grid(column=0, row=0)
self.header_label1 = ttk.Label(self.frame_header,
text="Please select who you are:")
self.header_label1.grid(column=1, row=0)
# Content frame
self.frame_content = ttk.Frame()
self.frame_content.pack()
self.frame_content.columnconfigure(0, weight=1)
self.content_tree = self.create_tree(self.frame_content)
self.users = []
db_conn = create_connection(self.database)
with db_conn:
users = get_users(db_conn)
for user in users:
self.users.append(User(id=user[0], username=user[1], balance=user[2]))
self.items_price_dict = get_products_list(db_conn)
# Add data to the treeview.
for user in self.users:
self.content_tree.insert('', tk.END, values=[user.username, user.balance])
self.button_add = ttk.Button(self,
text="Add User",
command=self.call_adduser_popup)
self.button_add.pack()
def create_tree(self, parent):
columns = ('name', 'balance')
style = Style()
font_size = 24
style.configure('Treeview.Heading', font='None, 28')
style.configure('Treeview', font=f'None, {font_size}', rowheight=int(font_size*1.6))
style.configure("Custom.Vertical.TScrollbar", arrowsize=60)
tree = ttk.Treeview(parent, columns=columns, show='headings', height=6)
tree.heading(columns[0], text='Name')
tree.column(columns[0], anchor=tk.CENTER, stretch=tk.NO, width=350)
tree.heading(columns[1], text='Balance')
tree.column(columns[1], anchor=tk.CENTER, stretch=tk.NO, width=350)
tree.bind('<<TreeviewSelect>>', self.user_selected)
tree.grid(column=0, row=0, pady=20)
scrollbar = ttk.Scrollbar(parent, orient=tk.VERTICAL, style="Custom.Vertical.TScrollbar", command=tree.yview)
tree.configure(yscroll=scrollbar.set)
scrollbar.grid(row=0, column=1, sticky='ns')
return tree
def user_selected(self, event):
selected_item = self.content_tree.selection()
user_idx = self.content_tree.index(selected_item[0])
amount, payment = self.call_items_popup()
if payment:
self.users[user_idx].pay_debt(amount)
elif amount == 0:
pass
else:
self.users[user_idx].calculate_debt(amount)
# Update the tree.
user = self.users[user_idx]
self.content_tree.item(selected_item[0], values=[user.username, user.balance])
# Update database.
db_conn = create_connection(self.database)
with db_conn:
update_user_debt(db_conn, user)
def exit(self):
pass
def call_items_popup(self):
# Get item price and return it.
return PopupWindowItems(self).get_price()
def call_adduser_popup(self):
name, balance = PopupNewUser(self).get_user()
if name == "":
return
self.update_new_user(name, balance)
def update_new_user(self, name, balance):
user = User(username=name, balance=balance)
db_conn = create_connection(self.database)
self.users = []
with db_conn:
add_user(db_conn, user)
users = get_users(db_conn)
for user in users:
self.users.append(User(id=user[0], username=user[1], balance=user[2]))
# Add data to the treeview.
self.content_tree.delete(*self.content_tree.get_children())
for user in self.users:
self.content_tree.insert('', tk.END, values=[user.username, user.balance])
class PopupWindowItems(tk.Toplevel):
value = 0
payment = False
def __init__(self, parent):
super().__init__(parent)
self.title("Select")
self.products_dict = parent.items_price_dict
label = ttk.Label(self, text="Select Product:")
label.pack(fill='x', padx=150, pady=5)
# Configure window appearance
self.attributes("-fullscreen", True)
# Configure button style
self.style = Style()
self.style.configure('TButton', font=('Helvetica', 24))
self.button_item = []
for item, price in self.products_dict.items():
display_string = f"{item:<18}\t\t{price} €"
self.button_item = ttk.Button(self, text=display_string, command=lambda m=price: self.get_selected_price(m))
self.button_item.pack(fill='x', ipady=6)
self.button_pay = ttk.Button(self,
text="Pay Debt!!",
style='info.TButton',
command=self.open_pay_popup)
self.button_pay.pack(fill='x', ipady=6)
self.button_close = ttk.Button(self,
text="Close",
style='danger.TButton',
command=self.destroy)
self.button_close.pack(fill='x', ipady=6)
def open_pay_popup(self):
self.value = PopupPay(self).get_value()
self.payment = True
self.destroy()
def get_selected_price(self, price):
self.value = price
self.destroy()
def get_price(self):
self.wait_window()
return self.value, self.payment
if __name__ == "__main__":
app = App()
style = Style()
app.mainloop()