So the issue is when I click the calc_button
, which calls the calculate_tip
method, I get a NameError
saying 'subtotal_entry' is not defined
. I have tried a bunch of different things to get this working but nothing has worked so far. I am trying to get the users input from the Entry
I created called subtotal_entry
.
Here is my code:
from tkinter import *
from tkinter import messagebox
class TipGUI:
def __init__(self,master):
self.master = master
master.title('Tip Calculator')
#create the frames
subtotal_frame = Frame(master).grid(row=0, column=0)
percent_frame = Frame(master).grid(row=1, column=0)
tip_frame = Frame(master).grid(row=2, column=0)
button_frame = Frame(master).grid(row=3, column=0)
total_frame = Frame(master).grid(row=4, column=0)
#create the labels
subtotal_label = Label(subtotal_frame, text="Enter the amount of the ticket:").grid(row=0, column=0)
percent_label = Label(percent_frame, text="Enter the tip as a percent:").grid(row=1, column=0)
tip_label = Label(tip_frame, text="Tip Amount: $").grid(row=2, column=0)
tipAmt_label = Label(tip_frame, text="").grid(row=2, column=1)
total_label = Label(total_frame, text="Total Amount: $").grid(row=3, column=0)
totalAmt_label = Label(total_frame, text="").grid(row=3, column=1)
#create entry boxes
subtotal_entry = Entry(subtotal_frame).grid(row=0, column=1)
percent_entry = Entry(percent_frame).grid(row=1, column=1)
#create buttons
calc_button = Button(button_frame, text="Calculate", command= self.calculate_tip).grid(row=4, column=0)
exit_button = Button(button_frame, text="exit", command= master.quit).grid(row=4, column=1)
def calculate_tip(self):
subtotal = Entry.get(subtotal_entry)
percent = percent_entry.get()
if subtotal <= 0:
messagebox.showinfo("Error","Please enter a subtotal greater than 0")
if percent <= 0:
messagebox.showinfo("Error","Please enter a tip percentage greater than 0")
else:
tip = subtotal * percent
total = tip + subtotal
tipAmt_label.config(text = format(tip, ',.2f'))
totalAmt_label.config(text = format(total, ',.2f'))
subtotal_entry.delete(0, END)
percent_entry.delete(0,END)
#Create the window
window = Tk()
tipGUI = TipGUI(window)
window.mainloop()