ValueError: invalid literal for int() with base 10 - TextInput value

151 views
Skip to first unread message

Strootaay Innovation Labs

unread,
May 31, 2020, 8:27:12 AM5/31/20
to Kivy users support
Hello. I have recently started learning Kivy and have encountered a weird error when handling text / numbers entered in a TextInput box.

The Kivy Code looks like this:

BoxLayout:
        orientation
: 'vertical'
       
BoxLayout:
            orientation
: 'horizontal'
           
Label:
                text
: 'No. of buttons:'
                markup
: True
                color
: 0, 0, 0, 1
           
CustomTextInput:
                id
: textinput_num
                max_characters
: 2
                multiline
: False
                input_filter
: 'int'

The relevant Python code looks like this:

class DropDownScreen(Screen):
   
def add_dd_values(self):
        dd_input
= App.get_running_app().root.get_screen('dd_screen').ids.textinput_num.text
       
print("TextInputBox: ", dd_input, "\n")
       
print("Length: ",len(dd_input))
       
print(int(dd_input)+1)

I have referenced the value of the text input box as follows:

dd_input = App.get_running_app().root.get_screen('dd_screen').ids.textinput_num.text

Here, textinput_num is the id of the relevant text input box. I am able to print the value of the text input box and also check the length of the string. Note: I have used the input_filter: 'int' statement to allow only numbers in the text input box. I understand that dd_input shall receive a string value. So, I tried to convert it into an integer value and perform a numerical operation.

However, I get the following error: ValueError: invalid literal for int() with base 10:

I looked up in Google and it seems that this is an issue with the data type being handled.

Can you please help me understand what is going wrong here? What is the right way to access and read numerical values from a text input box?

Elliot Garbus

unread,
May 31, 2020, 9:14:52 AM5/31/20
to kivy-...@googlegroups.com

There is not enough code there to see the issue.  What is the contents of dd_input when you get this error?

You can either use the input filter or overload the insert_text() method.  If you are overloading the insert_text method, the input_filter will have no effect.  Here is a small example using the input_filter set to ‘int’.  You can also use a callable (a method) as the input filter.

 

from kivy.app import App
from kivy.lang import Builder
from kivy.properties import NumericProperty
from kivy.uix.textinput import TextInput

kv =
"""
BoxLayout:
    orientation: 'vertical'
    FilterTextInput:
        id: ti
        input_filter: 'int'
        size_hint_y: None
        height: 48
        font_size: 20
        hint_text: 'Enter a number'
        multiline: False
    Label:
        text: ti.text
        font_size: 20
"""


class FilterTextInput(TextInput):

   
def on_text_validate(self):
       
print(f'The input: {self.text}')
       
print(f'text_input + 1 = {int(self.text) + 1}')

class TIFilterApp(App):

   
def build(self):
       
return Builder.load_string(kv)


TIFilterApp().run()

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/acb4ad8e-dcc5-49c3-918b-932f68d0482b%40googlegroups.com.

 

strootaay

unread,
May 31, 2020, 9:34:55 AM5/31/20
to Kivy users support
I see where I was going wrong. The textinput field was empty when I received the error. Essentially, dd_input was null or had an empty string, which could not be converted into an integer. When the textinput field had some value like 11 or 12, the conversion worked and the code was working fine. So foolish of me. :P

I changed the function code a bit:

class DropDownScreen(Screen):
   
def add_dd_values(self):
        dd_input
= App.get_running_app().root.get_screen('dropdown_screen').ids.textinput_num.text
       
print("TextInputBox: ", dd_input, "\n")
       
print("Length: ",len(dd_input))
       
print("Data Type: ", type(dd_input))
       
# Check if the text input box is empty and assign a default string of '0' so that int conversion and numerical operations do not fail with an error.
       
if dd_input == '':
            dd_input
="0"
       
print(int(dd_input)+1)

By the way, is this the correct way to access the text contents of a textinput field?

dd_input = App.get_running_app().root.get_screen('dropdown_screen').ids.textinput_num.text

Is there an easier or better approach?

To unsubscribe from this group and stop receiving emails from it, send an email to kivy-...@googlegroups.com.

Elliot Garbus

unread,
May 31, 2020, 10:22:27 AM5/31/20
to kivy-...@googlegroups.com

That is certainly a good way to access the text field.  Another option would be to put the text in to a kivy property, and then use that property.

 

from kivy.app import App
from kivy.lang import Builder
from kivy.properties import StringProperty
from kivy.uix.textinput import TextInput

kv =
"""

BoxLayout:
    orientation: 'vertical'
    FilterTextInput:
        id: ti
        input_filter: 'int'
        size_hint_y: None
        height: 48
        font_size: 20
        hint_text: 'Enter a number'
        multiline: False
        on_text: app.my_text = self.text  # when the text changes, copy to app.my_text

    Label:
        text: ti.text
        font_size: 20
    Button:
        size_hint_y: None
        height: 48
        text: 'Print text'
        on_release:
            print(f'Using the kivy property: {app.my_text}')
            print(f'using the widget id: {app.root.ids.ti.text}')

"""


class FilterTextInput(TextInput):
   
pass

class
TIFilterApp(App):
    my_text = StringProperty()

   
def build(self):
       
return Builder.load_string(kv)


TIFilterApp().run()

To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/2dfb5267-6d03-43a4-bb12-fe020e83287a%40googlegroups.com.

 

strootaay

unread,
Jun 1, 2020, 7:51:01 AM6/1/20
to Kivy users support
Thank you for the suggestion.
Reply all
Reply to author
Forward
0 new messages