Get number from a text box and use in a function?

Simple question: I have a text box and I have assigned the property as a number. I have created a function shown below which takes a user input and multiplies that number by 5. Cannot get this function to work,any assistance is greatly appreciated.

def equation(self):
    return self.tbx_hole_dia.text * 5

error message appears:
TypeError: unsupported operand type(s) for Mult: ‘NoneType’ and ‘int’

Welcome to Anvil.
(edited)

You have two problems here.

The first is that
self.tbx_hole_dia.text
is “None”, meaning it is not initialised or it is out of scope. If this function is in the same form that contains the text box, is the text box actually named “tbx_hole_dia”?

Secondly, if it did contain a value, you would be trying to multiply a string with a number.
Try this instead :

def equation(self):
  return int(self.tbx_hole_dia.text) * 5

The text box might be a “number” type, but that only affects the validation of the values it will accept. The “text” property is still exactly that, text.

Finally, when posting code please wrap it in triple backticks :
```
print(“code here”)
```
which will display like this :

print("code here")

Makes it much easier to read.

Hi David…firstly, thanks for the quick feedback! The text box is named “tbx_hole_dia” and I also tried your suggestion before this post:

‘’’
def equation(self):
return int(self.tbx_hole_dia.text) * 5
‘’’
…I invariably get the following error: TypeError: int() argument must be a string or a number, not ‘NoneType’

Ok, please could you post a clone link so I can take a look at your project?

To get the clone link, click the gear icon in your App browser, and you should see this :
image

Click “Share App” and look for “Share your source code and data with others”, and copy that link. You can either post it here if it’s non-sensitive or you can PM me if it is.

I’ll then be able to see what’s wrong.

https://anvil.works/build#clone:LO7MORUW64KEXJWP=PSQFM7OULYZZC7KTLPJCTHEB

Ok, I see the issue.

You are calling lug().equation()

replace that with self.equation().

You were essentially creating a new instance of the form class just for that function call, thus nothing was initialised.

Works!! Great catch! Again…thanks for the immediate assistance. Still trying the free version but looks like I will pulling the trigger soon on a paid version. Great product!!

2 Likes