Search code examples
pythonkivykivy-language

Kivy: How to change the text_hint value of a TextInput that's in a template


I'm trying to display some data by changing the text_hint value of a TextInput. If I print the values they are correct however I can't get them to update on the screen. Here's how I declare and use the the template in a .kv file.

<InformationBox@FloatLayout>
    lblTxtIn: 'Unknown Variable Name'
    txtInHint: "..."
    Label:
        text: root.lblTxtIn
        color: 235/255, 235/255, 235/255, 1
        pos_hint: {'center_x': 0.5, 'center_y': 0.7}
        bold: True
    TextInput:
        readonly: True
        hint_text: root.txtInHint
        multiline: False
        pos_hint: {'center_x': 0.5, 'center_y': 0.4}
        size_hint: (0.3, 0.25)
        hint_text_color: 0, 0, 0, 1
<MainMenu>:
    InformationBox:
        id: mylabel
        lblTxtIn: "Data Type Name"
        txtInHint: root.custom

And here's how I try to change the value in python.

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.clock import Clock
from kivy.properties import ObjectProperty, StringProperty
import random


class MainMenu(FloatLayout):
    custom = "0"


class MyMainApp(App):
    def build(self):
        return MainMenu()

    def txt_change(self, *args):
        MainMenu.custom = str(random.randrange(1, 10))
        print(MainMenu.custom)

    Clock.schedule_interval(txt_change, 1)

if __name__ == "__main__":
    MyMainApp().run()

I also tried to change it using ObjectProperty although then it shows an error telling that the object has no 'text_hint' attribute.

class MainMenu(FloatLayout):
    mylabel = ObjectProperty()

    def change_text(self, *args):
        MainMenu.mylabel.text_hint = "1"


class MyMainApp(App):
    def build(self):
        return MainMenu()

    Clock.schedule_interval(MainMenu.change_text, 1)

I'm a beginner and have no idea whether I'm doing a simple mistake or should approach the issue in a completely different way. I would be glad if someone could help me.


Solution

  • You can use the following approach to update the textinput field:

    from kivy.app import App
    from kivy.lang import Builder
    from kivy.uix.floatlayout import FloatLayout
    from kivy.clock import Clock
    import random
    
    
    APP_KV = """
    FloatLayout:
        lblTxtIn: "Data Type Name"
        txtInHint: "..."
        Label:
            text: root.lblTxtIn
            color: 235/255, 235/255, 235/255, 1
            pos_hint: {'center_x': 0.5, 'center_y': 0.7}
            bold: True    
        TextInput:
            id: mytxtinput
            readonly: True
            hint_text: root.txtInHint
            multiline: False
            pos_hint: {'center_x': 0.5, 'center_y': 0.4}
            size_hint: (0.3, 0.25)
            hint_text_color: 0, 0, 0, 1
    """
    
    class MyMainApp(App):
        def build(self):
            return Builder.load_string(APP_KV)
    
        def txt_change(self, *args):
            app.root.ids.mytxtinput.hint_text = str(random.randrange(1, 10))
            print(app.root.ids.mytxtinput.hint_text)
    
        Clock.schedule_interval(txt_change, 1)
    
    if __name__ == "__main__":
        app = MyMainApp()
        app.run()