First, connect your LineEdit signal, you can choose text_changed if you want to validate everytime user types something on it or text_entered if you want to validate it when user has finished typing.
On your signal function put this:
$LineEdit.text = str(float($LineEdit.text)
This will convert your text to float then reconvert it back to string again. However, it won’t tell user “Sorry, number only”, if you want to do that then you will have check every character and check their ord.
The ord of 0 is 48 and the ord of 9 is 57. So it will be like this
var text = $LineEdit.text
var i = 0
while text[i] != "":
if ord(text[i]) >= 48 and ord(text[i]) <= 57:
# Do something if current char is a number
else:
print("Sorry, number only")
# Do something if current char is not a number
i += 1
Thank you - I will look into this in detail.
mattkw80 | 2021-08-07 17:55
I found this to be a simple answer to what I was trying to do - thank you very much.
You should use a SpinBox, and get the number by going:
$SpinBox.value
With this method, the user can’t enter non-integer chars.
Hope this helps.
Thank you - I’m worried 1 to 1000 maybe too large of list to spin through, but I will look into this in detail. I’ve never used the SpinBox, so thank you, I’m excited to check it out.
mattkw80 | 2021-08-07 17:56
It’ll be fine if you make the SpinBox large enough! (eg. increasing the width so that you can fit the ‘1000’ in it). Note that you can do anything that you can do with LineEdits with SpinBoxes, EXCEPT enter strings, and you need to retrieve the number with .value rather than .text.