Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
GUI Tip Calculator
#1
I am working on a GUI tip calculator. The calculator needs to take entry from the user and have radio buttons to designate the tip percentage, display the tip amount and total cost. I keep getting this attribute error: "AttributeError: 'TipCalculator' object has no attribute 'meal_cost'"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from tkinter import *
 
class TipCalculator():
     
    def __init__(self):
        my_window = Tk()
         
        meal_cost = StringVar()
        tip_percent = IntVar()
        tip = StringVar()
        total_cost = StringVar()
 
        my_window.title("Tip Calculator")
         
        bill_amount_label = Label(text = "Bill Amount")
        bill_amount_label.grid(column = 2, row =1)
        bill_amount_entry = Entry(textvariable = meal_cost, width = 7, justify = RIGHT)
        bill_amount_entry.grid(column = 2, row = 2)
         
        tip_amounts = Label(text = "Tip Percentage")
        tip_amounts.grid(column = 1, row = 1)
        ten_percent_tip = Radiobutton(text = "10%", variable = tip_percent, value = 10)
        ten_percent_tip.grid(column = 1, row = 2)       
        fifteen_percent_tip = Radiobutton(text = "15%", variable = tip_percent, value = 15)
        fifteen_percent_tip.grid(column = 1, row = 3)       
        eighteen_percent_tip = Radiobutton(text = "18%", variable = tip_percent, value = 18)
        eighteen_percent_tip.grid(column = 1, row = 4)       
        twenty_percent_tip = Radiobutton(text = "20%", variable = tip_percent, value = 20)
        twenty_percent_tip.grid(column = 1, row = 5)     
         
        calculate_tip = Button(text = "Calculate Tip", command = self.calculate_tip)
        calculate_tip.grid(column = 2, row = 3)
 
        tip_amount_label = Label(text = "Tip Amount:")
        tip_amount_label.grid(column = 2, row = 4)
        tip_amount = Label(textvariable = tip, width = 7, justify = RIGHT)
        tip_amount.grid(column = 3, row = 4)
         
        bill_amount_label = Label(text = "Bill Total:")
        bill_amount_label.grid(column = 2, row = 5)
        bill_amount = Label(textvariable = total_cost, width = 7, justify = RIGHT)
        bill_amount.grid(column = 3, row = 5)
         
        my_window.mainloop()       
                 
     
    def calculate_tip(self):    
        pre_tip = float(self.meal_cost.get())
        percentage = self.tip_percent.get() / 100
        tip_amount = pre_tip * percentage
        self.tip.set(tip_amount)
         
        bill_total = pre_tip + tip_amount
        self.total_cost.set(bill_total)
     
 
TipCalculator()

Actually figured it out right after I posted this.
Reply
#2
Change the assignment in __init__ from
meal_cost = StringVar()
to
self.meal_cost = StringVar()

Change the attribute access in your class from meal_cost to self.meal_cost.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020