J Vazquez
J Vazquez

Reputation: 3

TypeError: float() argument must be a string or a number, not 'IntVar'

So Let me start off by saying that I am barely starting to learn python, so whatever error is in my code is due to this fact.

Anyways, I was trying to create a simple calculator for the volume of a sphere, so I went and created this VVV

import cmath

pi = cmath.pi

r = input("radius length: ")

V = 4 * pi * (float(r)**3)/3

print(V)

exit = input("Click enter to exit ")

yeah, i know, super simple right? Well, lets say I tried to use TKinter for making a more visually appealing version of this but failed miserably and don't know how.

This is the horribly written code VVV

from tkinter import *
import cmath

root = Tk()

r = IntVar()
pi = cmath.pi

askradius = Label(root, text="Enter radius")
askradius.grid(row="1", column="0")
radius = Entry(root, textvariable=r)
radius.grid(row="1", column="1")


V = 4 * pi * (float(r)**3)/3


Result = Label(root, text=V)
Result.grid(row="0", columnspan="2")


root.mainloop()

I know I didn't do a lot of stuff right, but bare in mind that this is just what I can come up with with the small amount of info I have access to. Whatever, to sum up what I think I was doing let me say I think I was "implementing the spere volume code into TKinter widgets, but since I barely know anything about TKinter, I messed up somewhere and got an error".

error says:

TypeError: float() argument must be a string or a number, not 'IntVar'

Upvotes: 0

Views: 3753

Answers (1)

DYZ
DYZ

Reputation: 57105

r is not a number, it is an object. You cannot convert it to a float, but you can convert its value. Change this:

V = 4 * pi * (float(r)**3)/3

to this:

V = 4 * pi * (float(r.get())**3)/3

Upvotes: 1

Related Questions