Search code examples
pythonuser-interfacetkinterttk

tkinter calculator in python


I am new to using both tkinter and python, and I am trying to create a GUI window thing that calculates the total cost of a date night, with different options for travel, entertainment, etc. I am having two major problems: 1. If the user attempts to enter a tip, nothing happens. The program works as long as the user does not try to leave a restaurant tip, though. I have tried reformatting, but I cannot seem to figure out why the way I have coded my tip isn't working. The tip is only supposed to be added if the restaurant option is selected. 2. This error is probably relatively simple, but I cannot figure out how to code a proper "quit" button. I have a few lines of code, which are my attempt to make a quit button after searching for solutions online, but I still seem to be missing something.

There is quite a bit of code, but I'll paste it here. I appreciate any advice or feedback :)

#import modules
import tkinter as tk
from tkinter import ttk

'''
calculate cost/make event handler for button push
'''

def calc_cost():
    #tax percent to decimal
    decimal = tax_result.get() / 100
    #if statement for tip
    if dinner_result.get() == 30:
        before_tax = travel_result.get() + entertainment_result.get() + dinner_result.get + ((int(tip_result.get()) / 100) * dinner_result)
    else:
        #find total before tax
        before_tax = travel_result.get() + entertainment_result.get() + dinner_result.get()
    #multiply tax decimal
    tax = before_tax * decimal
    #add to get total
    total_cost = before_tax + tax
    #assign 
    total_label["text"] = "$" + str(round(total_cost, 2))

#create window instance variable
app_window = tk.Tk()


'''
event handler quit button
'''
def close_window():
    app_window.destroy

'''
create radio options for travel
'''
#travel value variable
travel_result = tk.IntVar()

#frame shenanigans
travel_frame = ttk.Labelframe(app_window, text = "Travel")
travel_frame.grid(row = 1, column = 1, pady = 10, padx = 10)

#car radio button
car_radio = ttk.Radiobutton(travel_frame, variable = travel_result, value = 5)
car_radio.grid(row = 1, column = 1)
ttk.Label(travel_frame, text = "Car ($5)").grid(row = 1, column = 2, sticky = tk.W)

#bus radio button
bus_radio = ttk.Radiobutton(travel_frame, variable = travel_result, value = 1)
bus_radio.grid(row = 2, column = 1)
ttk.Label(travel_frame, text = "Bus ($1)").grid(row = 2, column = 2, sticky = tk.W)

#uber radio button
uber_radio = ttk.Radiobutton(travel_frame, variable = travel_result, value = 30)
uber_radio.grid(row = 3, column = 1)
ttk.Label(travel_frame, text = "Uber ($30)").grid(row = 3, column = 2, sticky = tk.W)

#limo radio button
limo_radio = ttk.Radiobutton(travel_frame, variable = travel_result, value = 200)
limo_radio.grid(row = 4, column = 1)
ttk.Label(travel_frame, text = "Limo ($200)").grid(row = 4, column = 2, sticky = tk.W)

'''
radio options for entertainment
'''

#entertainment value variable
entertainment_result = tk.IntVar()

#"framing" you for the crime ahaha
#jk just making a frame
entertainment_frame = ttk.Labelframe(app_window, text = "Entertainment")
entertainment_frame.grid(row = 2, column = 1, pady = 10, padx = 10)

#walk radio button
walk_radio = ttk.Radiobutton(entertainment_frame, variable = entertainment_result, value = 0)
walk_radio.grid(row = 1, column = 1)
ttk.Label(entertainment_frame, text = "Walk ($0)").grid(row = 1, column = 2, sticky = tk.W)

#mini golf radio button
golf_radio = ttk.Radiobutton(entertainment_frame, variable = entertainment_result, value = 10)
golf_radio.grid(row = 2, column = 1)
ttk.Label(entertainment_frame, text = "Mini Golf ($10)").grid(row = 2, column = 2, sticky = tk.W)

#movie radio button
#video killed the radio star :( RIP
movie_radio = ttk.Radiobutton(entertainment_frame, variable = entertainment_result, value = 26)
movie_radio.grid(row = 3, column = 1)
ttk.Label(entertainment_frame, text = "Movie ($26)").grid(row = 3, column = 2, sticky = tk.W)

'''
radio options for dinner
'''

#dinner value variable
dinner_result = tk.IntVar()

#dinner frame
dinner_frame = ttk.Labelframe(app_window, text = "Dinner")
dinner_frame.grid(row = 1, column = 2, pady = 10, padx = 10)

#fast food radio button
fast_radio = ttk.Radiobutton(dinner_frame, variable = dinner_result, value = 20)
fast_radio.grid(row = 1, column = 1)
ttk.Label(dinner_frame, text = "Fast Food ($20)").grid(row = 1, column = 2, sticky = tk.W)

#home made radio button
home_radio = ttk.Radiobutton(dinner_frame, variable = dinner_result, value = 15)
home_radio.grid(row = 2, column = 1)
ttk.Label(dinner_frame, text = "Home Made ($15)").grid(row = 2, column = 2, sticky = tk.W)

#restautant radio button
restaurant_radio = ttk.Radiobutton(dinner_frame, variable = dinner_result, value = 30)
restaurant_radio.grid(row = 3, column = 1)
ttk.Label(dinner_frame, text = "Restaurant ($30)").grid(row = 3, column = 2, sticky = tk.W)

'''
tax spinbox
'''

#tax variable
tax_result = tk.IntVar()

#frame
tax_frame = ttk.Labelframe(app_window, text = "Taxes in %")
tax_frame.grid(row = 2, column = 2, pady = 10, padx = 10)

#spinbox
tax_box = tk.Spinbox(tax_frame, from_=0, to=10, width = 5, justify = tk.RIGHT, textvariable = tax_result)
tax_box.grid(row = 1, column = 1, sticky = tk.W, pady = 3)

'''     
dinner tip scale 
'''

#result variable
tip_result = tk.IntVar()

# frame for the scale
tip_frame = ttk.Labelframe(app_window, text="Restaurant Tip Percent")
tip_frame.grid(row=1, column=3, pady=10, padx=10)

#tip scale range 1-35
tip_scale = tk.Scale(tip_frame, from_=1, to=35, var=tip_result, orient=tk.HORIZONTAL, length=100)
tip_scale.grid(row=1, column=3, sticky=tk.W)

'''                    
total result display
'''

#label variable
total_cost = tk.IntVar()

result_frame = ttk.Labelframe(app_window, text="Result")
result_frame.grid(row=2, column=3, pady=10, padx=10)

# label for total cost
total_cost = ttk.Label(result_frame, text="Total Cost:")
total_cost.grid(row=1, column=1)

#amount label
total_label = ttk.Label(result_frame, text="---", anchor="center")
total_label.grid(row=2, column=1)


'''                    
calculate button
'''
calc_button = tk.Button(app_window, text="Calculate Cost", command=calc_cost)
calc_button.grid(row=5, column=2)

'''                    
quit button
'''
quit_button = tk.Button(app_window, text="Quit", command=close_window)
quit_button.grid(row=5, column=3)


#wait for user
app_window.mainloop()

Also, I apologize for the weird comments that I put throughout. I deleted most of them, but I was tired when I made this and I thought everything was funny.


Solution

  • When I run code then I see error message which shows where is the problem and what is the problem. You should read it and think what wrong in this line. And you should also add this error message in question.

    Exception in Tkinter callback
    Traceback (most recent call last):
      File "/usr/lib/python3.7/tkinter/__init__.py", line 1705, in __call__
        return self.func(*args)
      File "<pyshell#1>", line 14, in calc_cost
        before_tax = travel_result.get() + entertainment_result.get() + dinner_result.get + ((int(tip_result.get()) / 100) * dinner_result)
    TypeError: unsupported operand type(s) for +: 'int' and 'method'
    

    Error shows line

    before_tax = travel_result.get() + entertainment_result.get() + dinner_result.get + ((int(tip_result.get()) / 100) * dinner_result)
    

    and in this line you forgot () in first dinner_result and you forgot .get() in second dinner_result

    It should be

    before_tax = travel_result.get() + entertainment_result.get() + dinner_result.get() + ((int(tip_result.get()) / 100) * dinner_result.get())
    

    and now it shows correct result for Restarunt


    As for button Quit - you forgot () in app_window.destroy()

    def close_window():
       app_window.destroy()