Как использовать переменную из текстового ввода в качестве текста для метки (kivy) - PullRequest
0 голосов
/ 03 августа 2020

Я обдумывал идею, которая у меня была для проекта, но одна часть, на которой я застрял, заключалась в сборе переменной из поля ввода текста и отображении ее значения в виде метки на другом экране. Я попытался поместить ее в переменную, а затем просто называя его текстом для метки в файле kivy, но он всегда возвращался как ошибка, что я делаю не так.

Kivy-

WindowManager:
    Enter_Name
    List


<Enter_Name>
    airline: input_1
    name: 'enter_name'
    id: enter_nom
    FloatLayout:
        cols: 3
        size: root.size
        Label:
            text: "Name of Airline?"
            size_hint: 1, 0.3
            pos_hint: {"x": 0, "top":1}

        TextInput:
            multiline: False
            name: 'input_one'
            id: input_1
            size_hint: 0.6, 0.06
            pos_hint: {"x": 0.20, "top":0.6}
        Button:
            size_hint: 0.2, 0.1
            pos_hint: {"x": 0.4, "top":0.4}
            text: "Enter"
            on_release: 
                app.root.current = 'list'
                root.line()

<List>
    name: 'list'

Python -

import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import ObjectProperty
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.lang import Builder
from kivy.uix.popup import Popup
import time
from kivy.properties import StringProperty

class Enter_Name(Screen):
    input_1 = StringProperty()
    def line(self):
        self.gon = self.ids.input_1.text
        print(self.gon)    
    pass

class List(Screen):
    Enter_Name.line
    pass

class WindowManager(ScreenManager):
    pass

kv = Builder.load_file("pot.kv")

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


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

1 Ответ

0 голосов
/ 03 августа 2020

Вот модифицированная версия вашего кода, которая делает то, что я думаю, вы хотите:

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

class Enter_Name(Screen):
    def line(self):
        App.get_running_app().root.get_screen('list').lab_text = self.airline.text

class List(Screen):
    pass

class WindowManager(ScreenManager):
    pass

# kv = Builder.load_file("pot.kv")
kv = Builder.load_string('''
WindowManager:
    Enter_Name
    List


<Enter_Name>
    airline: input_1
    name: 'enter_name'
    id: enter_nom
    FloatLayout:
        cols: 3
        size: root.size
        Label:
            text: "Name of Airline?"
            size_hint: 1, 0.3
            pos_hint: {"x": 0, "top":1}

        TextInput:
            multiline: False
            name: 'input_one'
            id: input_1
            size_hint: 0.6, 0.06
            pos_hint: {"x": 0.20, "top":0.6}
        Button:
            size_hint: 0.2, 0.1
            pos_hint: {"x": 0.4, "top":0.4}
            text: "Enter"
            on_release: 
                app.root.current = 'list'
                root.line()

<List>
    lab_text: ''
    name: 'list'
    Label:
        text: root.lab_text
''')

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


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

Я добавил Label к List Screen, а также lab_text свойство, которое используется как text для Label. Метод line() теперь просто устанавливает значение этого lab_text.

Я использовал load_string() вместо load_file() только для собственного удобства.

...