Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Tkinter and integer question

How do I convert a user-entry from an input box on Tkinter, into an integer? I keep getting "TypeError: unsupported operand type(s) for /: 'Radiobutton' and 'int'

boi
boi
14,242 Points

It would be helpful to solve your problem, if you could post your code.

1 Answer

Here is my code. I really was not sure what I was doing as I am just getting into coding, but my goal was to create a simple meal calculator. The user would select either a percent tip or a specific amount with a radiobutton, and then enter the value. The program runs, but in the end it won't calculate anything out.

from tkinter import *
import math

class MyFrame(Frame):
    def __init__(self):
        Frame.__init__(self)
        self.master.geometry("600x400")
        self.master.title("Restaurant Bill Calculator")
        self.grid()

        self.welcome_message = Label(self, text ="Welcome to the Tip Calculator!")
        self.welcome_message.grid(row = 1, column = 1)

        self.meal_cost

    def meal_cost(self):
        self.meal_cost = Label(self, text ="Please enter the total bill for your meal:")
        self.meal_cost.grid(row = 2, column = 1)

        self.meal_cost_input = IntVar()
        self.meal_cost_input = Entry(self)
        self.meal_cost_input.grid(row = 3, column = 1)

        self.submit_button = Button(self, text = "Submit", command = self.submit_click)
        self.submit_button.grid(row = 4, column = 1)

    def submit_click(self):
        self.message = Label(self, text = "Okay, meal cost was: $" + self.meal_cost_input.get())
        self.message.grid(row = 7, column = 1)

        self.tip_message = Label(self, text ="Next, choose how you would like to tip:")
        self.tip_message.grid(row = 8, column = 1)

        self.percent_tip = Radiobutton(self, text = "% tip", command = self.tip_calculate_percent)
        self.percent_tip.grid(row = 9, column = 1)

        self.amount_tip = Radiobutton(self, text = "Amount", command = self.tip_calculate_amount)
        self.amount_tip.grid(row = 9, column = 2)

    def tip_calculate_percent(self):
        self.percent_box = Label(text = "What percent would you like to tip?")
        self.percent_box.grid(row = 10, column = 1)

        self.percent_box_input = IntVar()
        self.percent_box_input = Entry(self)
        self.percent_box_input.grid(row = 11, column = 1)
        self.submit_button_two = Button(text = "Done", command = self.percent_done_click)
        self.submit_button_two.grid(row = 12, column = 1)


    def tip_calculate_amount(self):
        self.amount_box = Label(text = "How much would you like to tip?")
        self.amount_box.grid(row = 10, column = 1)

        self.amount_box_input = IntVar()
        self.amount_box_input = Entry(self)
        self.amount_box_input.grid(row = 11, column = 1)
        self.submit_button_two = Button(text = "Done", command = self.percent_done_click)
        self.submit_button_two.grid(row = 12, column = 1)


    def percent_done_click(self):
        self.total_price = self.meal_cost_input + (self.meal_cost_input) * (self.percent_tip / 100)
        self.total_price_box = Label(text = "Cost per person is: {}".format(total_price))
        self.total_price_box.grid(row = 13, column = 1)

    def amount_done_click(self):
        self.total_price = meal_cost_input + amount_tip
        self.total_price_box = Label(text = "Cost per person is: {}".format(total_price))
        self.total_price_box.grid(row = 14, column = 1)


frame02 = MyFrame()
frame02.mainloop()