Sunshine
Sunshine

Reputation: 191

Kivy: Updating TextInput when pressing Button

i'm trying to create a GUI with kivy. For this I would like to update the inputText when pushing the button within screen1. To organize my pages I'm using the a screen manager. What I'm trying to do basically is to input the value within the textInput with the id textbox as soon as the Button of this page is pressed. For this I started to prepare a class readInValues with the method read_in.

As soon as the button is preshed the InputText should change from Will be filled out automatically to some float extracted from a file.

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import StringProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.base import Builder

        
kv_string = Builder.load_string("""
<ScreenManagement>:
    MenuScreen:
        id: name
        name: 'menu'
    
    Screen1:
        id: screen1
        name: 'screen1'

<MenuScreen>:
    BoxLayout:
        BoxLayout:
            canvas:
                Color:
                    rgba: 184/255, 200/255, 202/255, 1
                Rectangle:
                    pos: self.pos
                    size: self.size
      
            GridLayout:
                cols: 4
                rows: 4
                
                BoxLayout:
                    orientation: 'horizontal'
                    spacing: 10
                    padding: 10
                                  
                    Button:
                        size_hint_y: None
                        pos: (10,150)
                        size_hint: None, None
                        size: 250,50
                        text: 'button1'
                        on_press: self.background_color =  0,168/255,137/255,1
                        on_release:
                            root.manager.current = 'screen1'
                            
<Screen1>:
    BoxLayout:
        orientation: 'vertical'
        Button:
            text: 'Return'
            on_press: self.background_color =  0,168/255,137/255,1
            on_release: root.manager.current = 'menu'
            pos: (0,0)
            size_hint: (1,.07)
            
        FloatLayout:
            BoxLayout:
                canvas:
                    Color:
                        rgba: 184/255, 200/255, 202/255, 1

                GridLayout:
                    cols: 2
                    Label:
                        text: 'field1:'
                    TextInput:
                        id: textbox
                        text: 'Will be filled out automatically'
                        background_color: 0,0,0,0.4
                    Label:
                        text: 'field2:'
                    TextInput:
                        id: x2
                        hint_text: 'Will be filled out automatically'
                        color: 1, 0,757, .145,1
                        background_color: 0,0,0,0.4
                    Label:
                        text: 'field3:'
                    TextInput:
                        id: x2
                        hint_text: 'Will be filled out automatically'
                        background_color: 0,0,0,0.4
                    Button:
                        text: 'fill out'
                        on_press:
                            root.callback(self.text)

""")

class readInValues(BoxLayout):
    def read_in(self, file = 'file1.npy'):
        value = load(file)
        return value
    
    
    def input_values(self):
        Input = self.ids.textbox
        Eingabe= self.read_in_weight()
        
            
        
class MenuScreen(Screen,readInValues):
    pass

class Screen1(Screen,readInValues):
    pass


class ScreenManagement(ScreenManager):
    pass

class MyApp(App):   
    def build(self):
        return ScreenManagement()
        
    

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

Upvotes: 0

Views: 1121

Answers (1)

John Anderson
John Anderson

Reputation: 39107

In your Screen1 class, add a method:

def callback(self, text)
    self.ids.textbox.text = text

I haven't tested this code, but I believe it should work. Of course, you can add more to that method.

Upvotes: 1

Related Questions