【问题标题】:How to pull a TextInput value into a different class and use it in Kivy?如何将 TextInput 值拉入不同的类并在 Kivy 中使用?
【发布时间】:2020-11-19 21:59:52
【问题描述】:

我只想从我的 Second Class 的文本输入中提取一个数值到我的 Home Class,然后能够在函数中处理它。我想我在这里很难掌握父子关系和语法。

请帮忙...

Python 文件:

    class Home(Screen):
    
        #Generic function
        def test(self):
    
            #grab numbers from text input here and be able to use the numeric value using ids
            #distance=..? I think normally it's self.root.distance.text
            print("distance text input variable here")
            pass
    
    class Second(Screen):
        pass  
    class WindowManager(ScreenManager):
        pass
   
    class HelpMe(App):
        def build(self):
            kv = Builder.load_file("help.kv")
            self.Home=Home()
            return kv
    
    if __name__ == "__main__":
        HelpMe().run()

kivy 文件:

WindowManager:
    Home:
    Second:
<Home>:
    name:"Home"
    GridLayout:
        cols:1
        Button:
            text:"Go"
            on_release:
                app.root.current="Second"
                root.manager.transition.direction="left"
<Second>:
    name:"Second"
    distance:distance
    GridLayout:
        cols:1
        TextInput:
            id:distance
            input_filter:"int"
        Button:
            text:"Back"
            #Initiate test code so I can work with values
            on_release:
                app.Home.test()
                app.root.current="Home"
                root.manager.transition.direction="right"

【问题讨论】:

    标签: python class kivy textinput


    【解决方案1】:

    您可以在单击按钮时将其从 kv 语言传递给测试功能:

    on_release:
        app.Home.test(distance)
        app.root.current="Home"
        root.manager.transition.direction="right"
    

    然后你在你的函数上阅读它:

    def test(self, dist):
        print(dist.text)
    

    记住它是以字符串的形式出现的。如果要用作数字,则必须将其更改为 int()。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-03
      • 1970-01-01
      • 1970-01-01
      • 2021-11-03
      • 1970-01-01
      • 2014-10-23
      • 1970-01-01
      • 2020-09-12
      相关资源
      最近更新 更多