-
- from tkinter import *
- from tkinter import messagebox
- from decimal import *
-
-
- class TipCalculatorInterface(Tk):
- def __init__(self, title):
- super().__init__()
-
- self.title(title)
- self.resizable(width=False, height=False)
-
- self.protocol("WM_DELETE_WINDOW", self.on_closing())
-
- self.main_frame = Frame(self)
-
- self.main_frame.grid(padx=14, pady=14)
-
- self.create_widgets()
-
- self.total_tip = 0
-
- def create_widgets(self):
-
- self.amount_label = Label(self.main_frame, text='Montant')
- self.amount_var = StringVar('')
- self.amount_entry = Entry(self.main_frame, textvariable=self.amount_var)
- self.amount_entry.focus()
- self.amount_label.grid(row=0, column=0, padx=5, pady=5, sticky=E)
- self.amount_entry.grid(row=0, column=1, padx=5, pady=5, sticky=W)
-
- self.tip_percentage_label = Label(self.main_frame, text='15%')
- self.tip_percentage_scale = Scale(
- self.main_frame,
- from_=0,
- to=100,
- orient=HORIZONTAL,
- showvalue=False,
- troughcolor='white',
- command=self.update_tip_percentage_label)
- self.tip_percentage_scale.set(15)
- self.tip_percentage_label.grid(
- row=1, column=0, padx=5, pady=5, sticky=E)
- self.tip_percentage_scale.grid(
- row=1, column=1, padx=5, pady=5, sticky=E + W)
-
- self.tip_label = Label(self.main_frame, text='Pourboire')
- self.tip_var = StringVar('')
- self.tip_entry = Entry(
- self.main_frame, state='readonly', textvariable=self.tip_var)
- self.tip_label.grid(row=2, column=0, padx=5, pady=5, sticky=E)
- self.tip_entry.grid(row=2, column=1, padx=5, pady=5, sticky=W)
-
- self.total_label = Label(self.main_frame, text='Total')
- self.total_var = StringVar('')
- self.total_entry = Entry(
- self.main_frame, state='readonly', textvariable=self.total_var)
- self.total_label.grid(row=3, column=0, padx=5, pady=5, sticky=E)
- self.total_entry.grid(row=3, column=1, padx=5, pady=5, sticky=W)
-
- self.calculate_button = Button(
- self.main_frame, text='Calculer', command=self.calculate_total)
- self.calculate_button.grid(
- row=4, column=1, padx=5, pady=5, sticky=E + W)
-
-
-
- def update_tip_percentage_label(self, value):
- self.tip_percentage_label['text'] = '{}%'.format(int(float(value)))
-
- def calculate_total(self):
- try:
- amount = Decimal(self.amount_var.get())
- tip = amount * Decimal(self.tip_percentage_scale.get()) / Decimal(100)
- self.total_tip += tip
- print(self.total_tip)
- total = amount + tip
- self.tip_var.set('{} $'.format(tip))
- self.total_var.set('{} $'.format(total))
- except InvalidOperation:
- self.amount_var.set('Entrez un montant valide')
- self.amount_entry.icursor(END)
-
- def on_closing(self):
- messagebox.showinfo('Here is the message')
-
-
- if __name__ == '__main__':
- TipCalculatorInterface('Calculateur de Pourboire').mainloop()
-