Python Forum
GUI Tip Calculator - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: GUI Tip Calculator (/thread-6331.html)



GUI Tip Calculator - djkraft - Nov-16-2017

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'"

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.


RE: GUI Tip Calculator - DeaD_EyE - Nov-16-2017

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.