Get value from a TextInput and use it in a function in a Python File

778 views
Skip to first unread message

ITDSS

unread,
Nov 26, 2018, 8:44:27 AM11/26/18
to Kivy users support
Hello,

I have a kivy file and a python file. I want to be able to get the text that I input into a TextInput in my kivy file, and to be able to use this input in a python file (so basically have it as a variable). I am quite certain it is possible, but so far I have had no luck. I have tried a lot of Googling as well as experimenting myself (I am quite new to Kivy though). 

The codes it is about:

.kv file:

TextInput:
        id: z_speed
        input_filter: 'float'
        font_size: '14sp'
        multiline: False
        size: 75, 25
        pos: 800, 110
    Label:
        text: "Speed Z-Axis (in mm/s)"
        font_size: '14sp'
        pos: 816, 95
    Button:
        text: "Apply"
        size: 50, 25
        pos: 885, 110
        background_down: 'pressed.png'
        on_release: background = 'standaard.png'
        on_press: root.SnelheidZ()

.py file:

def SnelheidZ(Widget):
        speed_3 = move_settings_t()
        speed_z = lib.get_move_settings(device_id_z, byref(speed_3))
        print("Snelheid Z-as in stappen: " + repr(speed_3.Speed))
        #what to put here? Maybe use ID of TextInput or something?

It might be possible with using an ID at the text input, but I just don't really know..

I hope someone can help me with this. I greatly appreciate any kind of help!

Patrick Vicens

unread,
Nov 26, 2018, 2:02:59 PM11/26/18
to Kivy users support
So you want to reference the .text  variable for the textinput widget. 

So if you build a page, you would reference page.ids.<name of widget goes here>.text

so self.ids.z_speed.text.


Message has been deleted

ITDSS

unread,
Nov 27, 2018, 5:56:43 AM11/27/18
to Kivy users support
Hello,

I have tried that and then it says self is not defined. So I give self as an argument in the function I am trying to call it in, and then it says it requires 1 positional argument in the place I call the function in the kivy file (with a button). It then says the following:

error.JPG


So I don't know how to fix this.

Greetings,

ITDSS      

Patrick Vicens

unread,
Nov 27, 2018, 10:52:18 AM11/27/18
to Kivy users support

Might help.  I think it might be because you aren't wrapping the widgets in your kv file under the defined widget in your python file. 

ITDSS

unread,
Nov 28, 2018, 2:24:08 AM11/28/18
to Kivy users support
Well, what I have is just one python file, with kivy code on the top. I did this so I could have as little files as possible. Everything else works completely fine. Maybe it's because of that, that it doesn't work as I want it to. But I just believe there should be a solution to this.

Elliot Garbus

unread,
Nov 28, 2018, 11:23:25 AM11/28/18
to Kivy users support
There are a few things that I see that are problematic.

Your root widget (TextInput), the widget furthest to the left in you Kv code, is TextInput.  Instead make this a layout, this will arrange and size all of the widgets under it.  I suggest a BoxLayout.
In your Python code you need to create a property(StringProperty).  You will use this to access data in Kv lang and Python.  These properties also create events, so when the text is updated an action will happen.

In the examples section of Kivy there is a great example called the Kivy Catalog.  Take a look at the text section.  You can modify the examples dynamically in the Kivy Catalog and see how changes to KV code change the appearance and operation.  

Elliot Garbus

unread,
Nov 28, 2018, 11:35:40 AM11/28/18
to Kivy users support
Looking again at your code in the top.  To access the text string associated with z_speed, in kv it is z_speed.text
in Python you would use self.ids.z_speed.text

Attach your single file example and I'll take a look.
Reply all
Reply to author
Forward
0 new messages