Gabe Panzerotti
Gabe Panzerotti

Reputation: 3

How can I use the value from a spin box (or other Tkinter widget) properly in a calculation?

I am writing a program in Python/Tkinter where I need to get the user's inputted value from a spin box and use it in a mathematical calculation (to calculate the cost of an item, more specifically). This is triggered by pressing a button.

from tkinter import *

root = Tk()

root.wm_title("Kiosk")
root.geometry("300x75")
root.resizable(0, 0)

popcorn = Spinbox(root, from_=0, to=10, state="readonly")
popcorn.pack()

def getvalue():
    print(popcorn.get()*9)

button = Button(root, text="Get value", command=getvalue)
button.pack()


root.mainloop()  

However, the problem I end up running into is the program not multiplying the numbers together, but printing the number nine times. The output when I click the button ends up something like "777777777". I set the spinbox to "readonly" so the user can't input text, only the values I assigned.

Obviously this isn't my entire project, just an example of what I'm trying to achieve.

Total newbie question, I know, but I can't seem to find the answer anywhere... Any help is appreciated.

Upvotes: 0

Views: 8786

Answers (1)

Sede
Sede

Reputation: 61225

popcorn.get() returns a string you need to convert it to integer using int or float point number using float.

def getvalue():
    print(int(popcorn.get()) * 9)

Upvotes: 1

Related Questions