ForyszeP
ForyszeP

Reputation: 163

How to change TextInput text after pressing button

What should I do to see 123 text after pressing Button 'Show 123'?

It works when I uncomment Clock.schedule_interval but I need this text to be editable. I see both prints "trigger" and "show" but the text '123' is not shown in the input.

I need it to work with the two classes.

from kivy.config import Config
Config.set('graphics', 'multisamples', '0')
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.clock import Clock
from kivy.uix.boxlayout import BoxLayout

kv = '''
#:import FadeTransition kivy.uix.screenmanager.FadeTransition
ScreenManagement:
    transition: FadeTransition()
    Show:

<First>:               
    Button:
        text: "Show 123"
        on_press: root.trigger_show()
<Second>:
    TextInput:
        id: textinput                
<Show>
    BoxLayout:
         First:
        Second:
'''

class ScreenManagement(ScreenManager):
    pass

class Show(Screen):
    pass

class First(BoxLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    def trigger_show(self):
        print("trigger")
        s = Second()
        s.show()

class Second(BoxLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        #Clock.schedule_interval(self.show, 1)

    def show(self): #(self, dt):
        print('show')
        self.ids.textinput.text = '123'

sm = Builder.load_string(kv)

class TestApp(App):
    def build(self):
        return sm

if __name__ == '__main__':
    TestApp().run()

Upvotes: 2

Views: 70

Answers (1)

eyllanesc
eyllanesc

Reputation: 243887

The Second created within trigger_show is different from the one created in the .kv, that is, it is setting the text in a widget that is not shown and is temporary since it is a local variable.

When creating classes you must expose the signals or events of internal elements throughout the class. In this case I will expose the event on_press of the Button to the First, and the text property of the TextInput to Second, then we make the connection in a scope where both elements exist:

from kivy.config import Config
Config.set('graphics', 'multisamples', '0')
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.boxlayout import BoxLayout

kv = '''
#:import FadeTransition kivy.uix.screenmanager.FadeTransition
ScreenManagement:
    transition: FadeTransition()
    Show:

<First>:               
    Button:
        text: "Show 123"
        on_press: root.dispatch('on_press')
<Second>:
    text: ""
    TextInput:
        id: textinput  
        text: root.text              
<Show>
    BoxLayout:
        First:
            on_press:
                second.text = "123"
        Second:
            id: second
'''

class ScreenManagement(ScreenManager):
    pass

class Show(Screen):
    pass

class First(BoxLayout):
    def __init__(self, **kwargs):
        self.register_event_type('on_press')
        super().__init__(**kwargs)

    def on_press(self):
        pass

class Second(BoxLayout):
    pass

sm = Builder.load_string(kv)

class TestApp(App):
    def build(self):
        return sm

if __name__ == '__main__':
    TestApp().run()

Upvotes: 3

Related Questions