如何引用对象

问题描述 投票:0回答:1

我想在 App 类中从类 Screen 中选择一个对象。

我写了一个想要执行的命令,但它在代码中不起作用。我希望代码在应用程序关闭时触发。

main.py:

'''

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

class Screeen(Screen):
    pass

class Manager(ScreenManager):
    pass

kv = Builder.load_file("plann.kv")
class Ball(App):
    #def on_stop(self):
        #print(Screeen.ids.textinput.text)

    def build(self):
        return kv

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

'''

plann.kv:

'''

Manager:
    Screeen:

<Screeen>:
    name: "Screeen"
    textinput: textinput
    FloatLayout:
        size: root.width, root.height
        TextInput:
            id: textinput
            text: "hi"
            size_hint: (.45, .1)
            pos: root.width * 1 / 3 , root.height * 1 / 2

'''

python kivy screen
1个回答
0
投票

您应该添加收到的错误消息。无论如何,您需要先初始化类,然后再使用

.run()
方法,如描述 here

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

class Screeen(Screen):
    pass

class Manager(ScreenManager):
    pass

kv = Builder.load_file("plann.kv")
class Ball(App):
    #def on_stop(self):
        #print(Screeen.ids.textinput.text)

    def build(self):
        return kv

if __name__ == "__main__":
    ball = Ball()
    ball().run()

我还建议将

v = Builder.load_file("plann.kv")
移到
build
方法中

© www.soinside.com 2019 - 2024. All rights reserved.