M
M
Mxrxlyn2022-04-01 04:54:44
Python
Mxrxlyn, 2022-04-01 04:54:44

How to read TextInput from kv into a variable?

I ran into a problem: In the idea of ​​​​the program, it is necessary to read the text from the input panel (TextInput) by pressing a button and transfer this text to the next screen using .format (essentially something like entering a name at the beginning of the game)

PY Code:

from kivy.config import Config

Config.set('graphics', 'height', 960)
Config.set('graphics', 'width', 540)
Config.set('graphics', 'resizable', 0)

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.uix.screenmanager import ScreenManager, Screen

class HomePage(Screen):
    pass


class MainPage(Screen):
    pass


class WinMan(ScreenManager):
    pass

kv = Builder.load_file('builder.kv')

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

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


KV code:
WinMan:
    HomePage:
    MainPage:

<HomePage>:
    name: 'home'

    GridLayout:
        cols: 1

        Image:
            source:'1600169626174216846.jpg'
            size_hint:(1, .703125)

        GridLayout:
            cols:1
            size_hint:(1, .15625)

            Label:
                text:'fffff'
                size_hint:(1, .15625)
                font_size:23
                halign:'left'
                valign:'middle'
                text_size:(520, 200)


        GridLayout:
            cols:1
            spacing:1
            padding:10
            size_hint:(1, .140625)

            TextInput:
                text:''
                size_hint:(1, .078125)
                multiline:False
                background_color: [.19607843137,.19607843137,.19607843137,1]

            Button:
                text:'Продолжить'
                size_hint:(1, 0.0625)
                background_color: [.196, .196, .196, .1]
                on_release: app.root.current = 'main'





<MainPage>:
    name: 'main'

    GridLayout:
        cols: 1

        Image:
            source:'doomer.jpg'
            size_hint:(1, .703125)

        GridLayout:
            cols:1
            size_hint:(1, .234375)

            Label:
                text:"ffff"
                size_hint:(1, .234375)
                font_size:23
                halign:'left'
                valign:'top'
                text_size:(520, 200)


        GridLayout:
            cols:1
            spacing:1
            padding:10
            size_hint:(1, .0625)


            Button:
                text:'Продолжить'
                size_hint:(1, .0625)
                background_color: [.196, .196, .196, .1]
                on_release: app.root.current = 'home'


I've already tried a lot of things. Something doesn't fit. I would be very grateful if someone could tell me what to do. Thank you in advance for your time and effort

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
Hcuy, 2022-04-02
@Hcuy

Let's say kivy code:

text_1 : text_1 
        TextInput:
            id: text_1
        Button:
            text: 'Save'
            on_press: 
                    root.text_input_1(text_1)

Then, to get the user's data, let's create the class Screen2_2 :
class Screen2_2(BoxLayout):
  def text_input_1(self, text_1):
    print (text_1.text)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question