Search code examples
pythonuser-inputkivy

Reading user-input data in Python 2.7 from Kivy file


Hopefully this will be an easy question for some of you. So I am working on some code to interface with a heater controller. The GUI is written mostly in Kivy language (to compartmentalize the various screens), but I have a .py file that ties all the Kivy files together. Within one my Kivy files, there are a few TextInput fields that are for a user to define a few numerical values. Once the text fields are filled, the user clicks on the "Start" button to begin the heater test. Now, my question is when using the "Start" button as my flag, how can I get Python to read the numerical values that are in the fields? I've looked online and posts about using the .get and raw_input methods, but nothing that has Python retrieving and interpreting values from Kivy code. Any help would be much appreciated!

This is part pf my .py file, which where I need the user-input values to be evaluated...

class ControllerScreen(Screen):

def build(self):
    return ControllerScreen()

def CreateExcelFile(self):
        wb = Workbook()
        ws = wb.active
        ws.cell('A1').value = 'Timestamp'
        ws.cell('B1').value = 'Temperature'
        ws.cell('C1').value = 'Batch 1'
        ws.cell(row=2, column=1).value = 000
        wb.save("Spreadsheet1.xlsx")

This is an excerpt from the Kivy file, containing the code for a single text field that a user types a number into...

<ControllerScreen>:
    TextInput:                                    #Ramp time increment field
        id: ramp_time
        font_size: 25
        size_hint: 0.1,0.06
        pos_hint: {"right":0.575, 'y':0.67}

    Button:                             #Start analysis button
        on_release: app.root.current = "home"
        text: "Start"
        font_size: 25
        color: 0,1,0.5,1
        size_hint: 0.29,0.075
        pos_hint: {'x':0.35, 'y':0.1}

And lastly, here is a picture of the GUI to give some reference to the text fields I am regarding (the one circled in red is the one I reference in the code above)...

GUI Interface


Solution

  • To access value of TextInput you need to use TextInput's property text, therefore to get a number you need to:

    n = int(self.ids.ramp_time.text) #or float or whatever you need
    

    However if you won't access the text in your ControllerScreen, then you need to manage a communication between your classes, so that you could use variables between them. You can achieve it with these answers: 1, 2 for example.

    There's an option for you to set a filtering for your input so your user won't screw something up(e.g. inputs letter/s)

    I've looked online and posts about using the .get and raw_input methods, but nothing that has Python retrieving and interpreting values from Kivy code.

    I think the problem is that you looked for getting user's input directly from python and not from Kivy. I'd say Kivy is quite new, so there will be no mention in old tutorials/websites. There are tutorials and docs directly for kivy either on official website or youtube.