discalc/discalc-noqty.py
2026-04-10 13:35:37 +07:00

48 lines
1.5 KiB
Python

import tkinter as tk
from tkinter import messagebox
def calculate():
try:
price = float(entry_price.get())
#quantity = int(entry_quantity.get())
rate = float(entry_rate.get())
if price < 0 or rate < 0: #price < 0 or quantity < 0 or rate < 0:
messagebox.showerror("Error", "Invalid input.", detail="Value cannot be negative.")
return
if rate > 100:
messagebox.showerror("Error", "Invalid input.", detail="Discount rate cannot exceed 100%.")
return
#subtotal = price * quantity
discount = price * rate / 100 #subtotal * rate / 100
total = price - discount #subtotal - discount
result_var.set(f"Total: {total:,.2f}")
except ValueError:
messagebox.showerror("Error", "Invalid input.", detail="Please enter numeric values. Fractions are allowed for price and discount rate.")
root = tk.Tk()
root.title("Discount Calculator")
root.geometry("280x250")
tk.Label(root, text="Price").pack(pady=5)
entry_price = tk.Entry(root, width=20)
entry_price.pack(pady=5)
# tk.Label(root, text="Quantity").pack(pady=5)
# entry_quantity = tk.Entry(root, width=20)
# entry_quantity.pack(pady=5)
tk.Label(root, text="Discount (%)").pack(pady=5)
entry_rate = tk.Entry(root, width=20)
entry_rate.pack(pady=5)
tk.Button(root, text="Calculate", command=calculate).pack(pady=10)
result_var = tk.StringVar()
tk.Label(root, textvariable=result_var).pack(pady=10)
root.mainloop()