Module calculate_discount.main

Main module for the Discount Calculator GUI application.

Functions

def main()
Expand source code
def main():
    """
    Launch the Discount Calculator GUI application.

    Creates a Tkinter window with input fields for purchase amount and discount percentage.
    Provides buttons to calculate the discounted price and to exit the application.

    Args:
        None

    Returns:
        None
    """
    root = tk.Tk()
    root.title("СкидкаКалькулятор v1")
    root.geometry("400x300")

    tk.Label(root, text="Сумма покупки:").pack(pady=5)
    amount_entry = tk.Entry(root)
    amount_entry.pack(pady=5)

    tk.Label(root, text="Процент скидки:").pack(pady=5)
    discount_entry = tk.Entry(root)
    discount_entry.pack(pady=5)

    result_label = tk.Label(root, text="Результат: ", fg="blue")
    result_label.pack(pady=10)

    def calc_and_show():
        """
        Retrieve input values, validate them, calculate the discounted price, and display the result.

        This function is called when the "Посчитать скидку" button is pressed.
        It handles ValueError exceptions from non-numeric input and shows an error message if the data is invalid.

        Args:
            None

        Returns:
            None
        """
        try:
            amt = float(amount_entry.get())
            dsc = float(discount_entry.get())
            if amt < 0 or dsc < 0 or dsc > 100:
                messagebox.showerror("Ошибка", "Некорректные данные!")
                return
            res = calculate_discount(amt, dsc)
            result_label.config(text=f"Результат: {res:.2f} руб.")
        except ValueError:
            messagebox.showerror("Ошибка", "Вводите числа!")

    calc_btn = tk.Button(root, text="Посчитать скидку", command=calc_and_show)
    calc_btn.pack(pady=10)

    exit_btn = tk.Button(root, text="Выход", command=sys.exit)
    exit_btn.pack(pady=5)

    root.mainloop()

Launch the Discount Calculator GUI application.

Creates a Tkinter window with input fields for purchase amount and discount percentage. Provides buttons to calculate the discounted price and to exit the application.

Args

None

Returns

None