add / subtract paid amount in cash buttons
This commit is contained in:
2 files changed
+174
-16
No files matched your search
+49
-16
@@ -1,7 +1,7 @@
|
|||||||
#!/usr/bin/env python3.11
|
#!/usr/bin/env python3.11
|
||||||
from customtkinter import set_appearance_mode, set_default_color_theme
|
from customtkinter import set_appearance_mode, set_default_color_theme
|
||||||
from customtkinter import CTk, CTkEntry, CTkButton, CTkLabel, CTkFrame
|
from customtkinter import CTk, CTkEntry, CTkButton, CTkLabel, CTkFrame, CTkSwitch
|
||||||
from customtkinter import DISABLED
|
from currencyButtons import initializeCurrencyButtons
|
||||||
|
|
||||||
class Window(CTk):
|
class Window(CTk):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@@ -19,45 +19,72 @@ class Window(CTk):
|
|||||||
|
|
||||||
self.amountDue: float = 0.0
|
self.amountDue: float = 0.0
|
||||||
|
|
||||||
self.left_frame: CTkFrame = CTkFrame(master=self, width=((self.width / 2) - 20), height=(self.height - 40))
|
self.left_frame: CTkFrame = CTkFrame(master=self, width=(self.width / 2 - 20), height=(self.height / 2 - 40), fg_color="gray20")
|
||||||
self.left_frame.grid_propagate(False)
|
self.left_frame.grid_propagate(False)
|
||||||
self.left_frame.grid(row=0, column=0, pady=20, padx=10)
|
self.left_frame.grid(row=0, column=0, pady=20, padx=10, sticky='NW')
|
||||||
|
|
||||||
# Static label
|
# Static label
|
||||||
_mainLabel: CTkLabel = CTkLabel(master=self.left_frame, text="Change Due Calculator")
|
mainLabel: CTkLabel = CTkLabel(master=self.left_frame, text="Change Due Calculator", font=("Arial", 18, 'bold'))
|
||||||
_mainLabel.grid(row=0, column=0, pady=12, padx=10)
|
mainLabel.grid(row=0, column=0, pady=12, padx=10)
|
||||||
|
|
||||||
charValidation = self.register(self.only_numbers)
|
charValidation = self.register(self.only_numbers)
|
||||||
self.entry: CTkEntry = CTkEntry(
|
self.entry: CTkEntry = CTkEntry(
|
||||||
master=self.left_frame,
|
master=self.left_frame,
|
||||||
placeholder_text="Amount Charged...",
|
|
||||||
width=200,
|
width=200,
|
||||||
height=40,
|
height=40,
|
||||||
validate="key",
|
validate="key",
|
||||||
validatecommand=(charValidation, '%d', '%s', '%S')
|
validatecommand=(charValidation, '%d', '%s', '%S'),
|
||||||
|
placeholder_text="Amount Charged..."
|
||||||
)
|
)
|
||||||
self.entry.grid(row=1, column=0, pady=12, padx=10)
|
self.entry.grid(row=1, column=0, pady=12, padx=10)
|
||||||
|
self.entry.insert(0, 'Amount Charged...')
|
||||||
|
|
||||||
# Warning Label
|
# Warning Label
|
||||||
self.invalidLabel: CTkLabel = CTkLabel(master=self.left_frame, text="Not a Number!", text_color="red", font=("Arial", 24, 'bold'))
|
self.invalidLabel: CTkLabel = CTkLabel(master=self.left_frame, text="Not a Number!", text_color="red", font=("Arial", 20, 'bold'))
|
||||||
|
|
||||||
calculateButton: CTkButton = CTkButton(master=self.left_frame, text="Calculate", command=self.calculate, width=200, height=50)
|
calculateButton: CTkButton = CTkButton(master=self.left_frame, text="Calculate", command=self.calculate, width=200, height=50)
|
||||||
calculateButton.grid(row=2, column=0, pady=12, padx=10)
|
calculateButton.grid(row=2, column=0, pady=12, padx=10)
|
||||||
|
|
||||||
self.right_frame: CTkFrame = CTkFrame(master=self, width=((self.width / 2) - 20), height=(self.height - 40))
|
self.left_frame_bottom: CTkFrame = CTkFrame(master=self, width=(self.width / 2 - 20), height=(self.height / 2 - 40), fg_color="gray20")
|
||||||
|
self.left_frame_bottom.grid_propagate(False)
|
||||||
|
self.left_frame_bottom.grid(row=0, column=0, pady=20, padx=10, sticky='SW')
|
||||||
|
|
||||||
|
paidWithLabel: CTkLabel = CTkLabel(master=self.left_frame_bottom, text="Paid With", font=("Arial", 18, 'bold'))
|
||||||
|
paidWithLabel.grid(row=0, column=0, pady=(16, 0), padx=10)
|
||||||
|
|
||||||
|
self.mode: bool = True # True == ADD, False == SUBTRACT
|
||||||
|
self.modeLabel: CTkLabel = CTkLabel(master=self.left_frame_bottom, text=f"Mode: {'ADD' if self.mode else 'SUBTRACT'}", font=("Arial", 18, 'bold'))
|
||||||
|
self.modeLabel.grid(row=0, column=4, pady=(16, 0), padx=(50, 10), sticky='NE')
|
||||||
|
|
||||||
|
self.modeSelect: CTkSwitch = CTkSwitch(master=self.left_frame_bottom, text="Mode", command=self.updateMode)
|
||||||
|
self.modeSelect.grid(row=0, column=3, pady=(16, 0), padx=(50, 10), sticky='NE')
|
||||||
|
|
||||||
|
self.total: float = 0.0
|
||||||
|
self.totalLabel: CTkLabel = CTkLabel(master=self.left_frame_bottom, text=f"Total: {self.total}", font=("Arial", 18, 'bold'))
|
||||||
|
self.totalLabel.grid(row=0, column=2, pady=(16, 0), padx=10, sticky='NE')
|
||||||
|
|
||||||
|
currencyButtons: list[CTkButton] = initializeCurrencyButtons(self.left_frame_bottom, self.sum)
|
||||||
|
|
||||||
|
r, c = 1, 0
|
||||||
|
for button in currencyButtons:
|
||||||
|
button.grid(row=r, column=c, pady=12, padx=10)
|
||||||
|
if c < 3: c += 1
|
||||||
|
else: c = 0; r += 1
|
||||||
|
|
||||||
|
self.right_frame: CTkFrame = CTkFrame(master=self, width=(self.width / 2 - 20), height=(self.height - 40), fg_color="gray20")
|
||||||
self.right_frame.grid_propagate(False)
|
self.right_frame.grid_propagate(False)
|
||||||
self.right_frame.grid(row=0, column=1, pady=20, padx=10)
|
self.right_frame.grid(row=0, column=1, pady=20, padx=10)
|
||||||
|
|
||||||
self.total: float = 0.0
|
|
||||||
self.totalLabel: CTkLabel = CTkLabel(master=self.right_frame, text=f"Total: {self.total}", font=("Arial", 24, 'bold'))
|
|
||||||
self.totalLabel.grid(row=0, column=0, pady=12, padx=10)
|
|
||||||
|
|
||||||
self.mainloop()
|
self.mainloop()
|
||||||
|
|
||||||
|
def updateMode(self) -> None:
|
||||||
|
self.mode = not self.mode
|
||||||
|
self.modeLabel.configure(text=f"Mode: {'ADD' if self.mode else 'SUBTRACT'}")
|
||||||
|
|
||||||
def quitApplication(self, event=None) -> None: self.destroy()
|
def quitApplication(self, event=None) -> None: self.destroy()
|
||||||
|
|
||||||
def only_numbers(self, action: int, current: str, char: str) -> bool:
|
def only_numbers(self, action: str, current: str, char: str) -> bool:
|
||||||
if action == 0: return True # If delete character
|
if int(action) == 0: return True # If delete character
|
||||||
if '.' in current and len(current.split('.')[1]) == 2: return False # prevent more than 2 decimal
|
if '.' in current and len(current.split('.')[1]) == 2: return False # prevent more than 2 decimal
|
||||||
return (char.isdigit() or char == ".") # only allow digits and decimal
|
return (char.isdigit() or char == ".") # only allow digits and decimal
|
||||||
|
|
||||||
@@ -77,5 +104,11 @@ class Window(CTk):
|
|||||||
def calculate(self) -> None:
|
def calculate(self) -> None:
|
||||||
print(self.amountDue)
|
print(self.amountDue)
|
||||||
|
|
||||||
|
def sum(self, value: float) -> None:
|
||||||
|
if self.total == 0 and not self.mode: return
|
||||||
|
self.total += value * (1 if self.mode else -1)
|
||||||
|
if self.total < 0: self.total = 0
|
||||||
|
self.totalLabel.configure(text=f"Total: {self.total:.2f}")
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
window = Window()
|
window = Window()
|
||||||
@@ -0,0 +1,125 @@
|
|||||||
|
from customtkinter import CTkButton
|
||||||
|
|
||||||
|
def initializeCurrencyButtons(root, cmd) -> list[CTkButton]:
|
||||||
|
return [
|
||||||
|
CTkButton(
|
||||||
|
master=root,
|
||||||
|
text="$100 Bill",
|
||||||
|
width=152,
|
||||||
|
height=70,
|
||||||
|
font=("Arial", 24, 'bold'),
|
||||||
|
fg_color="Burlywood3",
|
||||||
|
hover_color="Burlywood4",
|
||||||
|
command=lambda: cmd(100)
|
||||||
|
),
|
||||||
|
|
||||||
|
CTkButton(
|
||||||
|
master=root,
|
||||||
|
text="$50 Bill",
|
||||||
|
width=152,
|
||||||
|
height=70,
|
||||||
|
font=("Arial", 24, 'bold'),
|
||||||
|
fg_color="IndianRed3",
|
||||||
|
hover_color="IndianRed4",
|
||||||
|
command=lambda: cmd(50)
|
||||||
|
),
|
||||||
|
|
||||||
|
CTkButton(
|
||||||
|
master=root,
|
||||||
|
text="$20 Bill",
|
||||||
|
width=152,
|
||||||
|
height=70,
|
||||||
|
font=("Arial", 24, 'bold'),
|
||||||
|
fg_color="medium sea green",
|
||||||
|
hover_color="sea green",
|
||||||
|
command=lambda: cmd(20)
|
||||||
|
),
|
||||||
|
|
||||||
|
CTkButton(
|
||||||
|
master=root,
|
||||||
|
text="$10 Bill",
|
||||||
|
width=152,
|
||||||
|
height=70,
|
||||||
|
font=("Arial", 24, 'bold'),
|
||||||
|
fg_color="MediumPurple3",
|
||||||
|
hover_color="MediumPurple4",
|
||||||
|
command=lambda: cmd(10)
|
||||||
|
),
|
||||||
|
|
||||||
|
CTkButton(
|
||||||
|
master=root,
|
||||||
|
text="$5 Bill",
|
||||||
|
width=152,
|
||||||
|
height=70,
|
||||||
|
font=("Arial", 24, 'bold'),
|
||||||
|
fg_color="royal blue",
|
||||||
|
hover_color="dark slate blue",
|
||||||
|
command=lambda: cmd(5)
|
||||||
|
),
|
||||||
|
|
||||||
|
CTkButton(
|
||||||
|
master=root,
|
||||||
|
text="$2 Coin",
|
||||||
|
width=152,
|
||||||
|
height=70,
|
||||||
|
font=("Arial", 24, 'bold'),
|
||||||
|
fg_color="honeydew4",
|
||||||
|
hover_color="gray37",
|
||||||
|
command=lambda: cmd(2)
|
||||||
|
),
|
||||||
|
|
||||||
|
CTkButton(
|
||||||
|
master=root,
|
||||||
|
text="$1 Coin",
|
||||||
|
width=152,
|
||||||
|
height=70,
|
||||||
|
font=("Arial", 24, 'bold'),
|
||||||
|
fg_color="lightgoldenrod4",
|
||||||
|
hover_color="tan4",
|
||||||
|
command=lambda: cmd(1)
|
||||||
|
),
|
||||||
|
|
||||||
|
CTkButton(
|
||||||
|
master=root,
|
||||||
|
text="Quarter ($0.25)",
|
||||||
|
width=152,
|
||||||
|
height=70,
|
||||||
|
font=("Arial", 19, 'bold'),
|
||||||
|
fg_color="honeydew4",
|
||||||
|
hover_color="gray37",
|
||||||
|
command=lambda: cmd(0.25)
|
||||||
|
),
|
||||||
|
|
||||||
|
CTkButton(
|
||||||
|
master=root,
|
||||||
|
text="Dime ($0.10)",
|
||||||
|
width=152,
|
||||||
|
height=70,
|
||||||
|
font=("Arial", 19, 'bold'),
|
||||||
|
fg_color="honeydew4",
|
||||||
|
hover_color="gray37",
|
||||||
|
command=lambda: cmd(0.1)
|
||||||
|
),
|
||||||
|
|
||||||
|
CTkButton(
|
||||||
|
master=root,
|
||||||
|
text="Nickel ($0.05)",
|
||||||
|
width=152,
|
||||||
|
height=70,
|
||||||
|
font=("Arial", 19, 'bold'),
|
||||||
|
fg_color="honeydew4",
|
||||||
|
hover_color="gray37",
|
||||||
|
command=lambda: cmd(0.05)
|
||||||
|
),
|
||||||
|
|
||||||
|
CTkButton(
|
||||||
|
master=root,
|
||||||
|
text="Penny ($0.01)",
|
||||||
|
width=152,
|
||||||
|
height=70,
|
||||||
|
font=("Arial", 19, 'bold'),
|
||||||
|
fg_color="saddle brown",
|
||||||
|
hover_color="OrangeRed4",
|
||||||
|
command=lambda: cmd(0.01)
|
||||||
|
),
|
||||||
|
]
|
||||||
Reference in new issue
Block a user