spacepaste

  1.  
  2. from tkinter import *
  3. from tkinter import messagebox
  4. from decimal import *
  5. class TipCalculatorInterface(Tk):
  6. def __init__(self, title):
  7. super().__init__()
  8. self.title(title)
  9. self.resizable(width=False, height=False)
  10. self.protocol("WM_DELETE_WINDOW", self.on_closing())
  11. self.main_frame = Frame(self)
  12. self.main_frame.grid(padx=14, pady=14)
  13. self.create_widgets()
  14. self.total_tip = 0
  15. def create_widgets(self):
  16. self.amount_label = Label(self.main_frame, text='Montant')
  17. self.amount_var = StringVar('')
  18. self.amount_entry = Entry(self.main_frame, textvariable=self.amount_var)
  19. self.amount_entry.focus()
  20. self.amount_label.grid(row=0, column=0, padx=5, pady=5, sticky=E)
  21. self.amount_entry.grid(row=0, column=1, padx=5, pady=5, sticky=W)
  22. self.tip_percentage_label = Label(self.main_frame, text='15%')
  23. self.tip_percentage_scale = Scale(
  24. self.main_frame,
  25. from_=0,
  26. to=100,
  27. orient=HORIZONTAL,
  28. showvalue=False,
  29. troughcolor='white',
  30. command=self.update_tip_percentage_label)
  31. self.tip_percentage_scale.set(15)
  32. self.tip_percentage_label.grid(
  33. row=1, column=0, padx=5, pady=5, sticky=E)
  34. self.tip_percentage_scale.grid(
  35. row=1, column=1, padx=5, pady=5, sticky=E + W)
  36. self.tip_label = Label(self.main_frame, text='Pourboire')
  37. self.tip_var = StringVar('')
  38. self.tip_entry = Entry(
  39. self.main_frame, state='readonly', textvariable=self.tip_var)
  40. self.tip_label.grid(row=2, column=0, padx=5, pady=5, sticky=E)
  41. self.tip_entry.grid(row=2, column=1, padx=5, pady=5, sticky=W)
  42. self.total_label = Label(self.main_frame, text='Total')
  43. self.total_var = StringVar('')
  44. self.total_entry = Entry(
  45. self.main_frame, state='readonly', textvariable=self.total_var)
  46. self.total_label.grid(row=3, column=0, padx=5, pady=5, sticky=E)
  47. self.total_entry.grid(row=3, column=1, padx=5, pady=5, sticky=W)
  48. self.calculate_button = Button(
  49. self.main_frame, text='Calculer', command=self.calculate_total)
  50. self.calculate_button.grid(
  51. row=4, column=1, padx=5, pady=5, sticky=E + W)
  52. def update_tip_percentage_label(self, value):
  53. self.tip_percentage_label['text'] = '{}%'.format(int(float(value)))
  54. def calculate_total(self):
  55. try:
  56. amount = Decimal(self.amount_var.get())
  57. tip = amount * Decimal(self.tip_percentage_scale.get()) / Decimal(100)
  58. self.total_tip += tip
  59. print(self.total_tip)
  60. total = amount + tip
  61. self.tip_var.set('{} $'.format(tip))
  62. self.total_var.set('{} $'.format(total))
  63. except InvalidOperation:
  64. self.amount_var.set('Entrez un montant valide')
  65. self.amount_entry.icursor(END)
  66. def on_closing(self):
  67. messagebox.showinfo('Here is the message')
  68. if __name__ == '__main__':
  69. TipCalculatorInterface('Calculateur de Pourboire').mainloop()
  70.