我怎样才能使 textinput 成为 class 变量

How can i make textinput a class variable

我正在尝试从另一个屏幕获取 TextInput 的文本。在下面的代码中,我可以获得文本,但我的项目有多个功能。所以我想如果我可以让 textinput 成为一个 class 变量,它会更容易编码。

示例代码:

.py

from kivy.app import App
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.uix.screenmanager import NoTransition
import requests


class MainPage(Screen):
    pass


class ExercisePage(Screen):

    def test(self):
        link = self.manager.get_screen('mainpage')
        a = requests.get(f'{link.ids.http.text}/gpio12On')

    def testt(self):
        link = self.manager.get_screen('mainpage')
        a = requests.get(f'{link.ids.http.text}/gpio12Off')


class MyApp(App):

    def build(self):
        global sm
        sm = ScreenManager(transition=NoTransition())
        sm.add_widget(MainPage(name='mainpage'))
        sm.add_widget(ExercisePage(name='exercisepage'))
        return sm


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

这里是 .kv 文件

<MainPage>
    FloatLayout:
        TextInput:
            id: http
            text: ""
            multiline: False
            size_hint: 0.5,0.05
            pos_hint: {"x": 0.3, "y": 0.8}
        Button:
            text:"Enter"
            size_hint: 0.1,0.06
            pos_hint: {"x": 0.82, "y": 0.795}
            on_release:
                app.root.current = "exercisepage"

<ExercisePage>
    FloatLayout:
        Button:
            text:"test"
            size_hint: 0.5,0.075
            pos_hint: {"x": 0.2, "top": 0.4}
            on_release:
                root.test()
        Button:
            text:"test2"
            size_hint: 0.5,0.075
            pos_hint: {"x": 0.2, "top": 0.5}
            on_release:
                root.testt()

我想做的是这样的:

class ExercisePage(Screen):
    link = self.manager.get_screen('mainpage')
    
    def test(self):
        a = requests.get(f'{self.link.ids.http.text}/gpio12On')

    def testt(self):
        a = requests.get(f'{self.link.ids.http.text}/gpio12Off')

如果您在 kv 文件中定义应用程序 root,则 ids 在该 root 规则中随处可用。然后你可以在 ExercisePage 中定义一个 ObjectProperty 来引用 TextInput text。下面是这样一个 kv 文件的样子:

ScreenManager:
    MainPage:
        name: 'mainpage'
        FloatLayout:
            TextInput:
                id: http
                text: ""
                multiline: False
                size_hint: 0.5,0.05
                pos_hint: {"x": 0.3, "y": 0.8}
            Button:
                text:"Enter"
                size_hint: 0.1,0.06
                pos_hint: {"x": 0.82, "y": 0.795}
                on_release:
                    app.root.current = "exercisepage"  # this could also be just root.current = "exercisepage"
    
    ExercisePage:
        id: escr
        link: http.text  # link to the TextInput text
        name: 'exercisepage'
        FloatLayout:
            Button:
                text:"test"
                size_hint: 0.5,0.075
                pos_hint: {"x": 0.2, "top": 0.4}
                on_release:
                    escr.test()  # changed to use id
            Button:
                text:"test2"
                size_hint: 0.5,0.075
                pos_hint: {"x": 0.2, "top": 0.5}
                on_release:
                    escr.testt()  # changed to use id

然后在 ExercisePage 中定义 ObjectProperty,并在方法中使用它:

class ExercisePage(Screen):
    link = ObjectProperty(None)

    def test(self):
        # link = self.manager.get_screen('mainpage')
        print(self.link)
        # a = requests.get(f'{link.ids.http.text}/gpio12On')

    def testt(self):
        # link = self.manager.get_screen('mainpage')
        print(self.link)
        # a = requests.get(f'{link.ids.http.text}/gpio12Off')

并且由于您在 kv 文件中定义了 root 小部件,因此您可以从 MyApp class 中完全消除 build() 方法。