简体   繁体   English

没有 .kv 文件的 Kivy ScreenManager

[英]Kivy ScreenManager without .kv file

i'm trying to make a screen transition without a .kv file.我正在尝试在没有 .kv 文件的情况下进行屏幕转换。 Is it possible to do it like this without .kv?没有.kv可以这样做吗? Here is the code这是代码

class ScreenManagement(ScreenManager):
    def __init__(self, **kwargs):
        super(ScreenManagement, self).__init__(**kwargs)


class testW(Screen):
    def __init__(self, **kwargs):
        super(testW, self).__init__()

class emotionRecog(Screen):
    def __init__(self, **kwargs):
        super(emotionRecog, self).__init__(**kwargs)
        self.button1 = Button(text="Next", size_hint=(1, .1))
        self.button1.bind(on_press=self.screenTransition())
        layout = BoxLayout(orientation="vertical")
        layout.add_widget(self.button1)
        return layout

    def screenTransition(self, *args):
        self.manager.current = 'test'


class CamApp(App):
    def build(self):
        sm = ScreenManagement(transition=FadeTransition())
        sm.add_widget(emotionRecog(name='emotion'))
        sm.add_widget(testW(name='test'))
        return sm


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

After running it i get these error运行后我得到这些错误

     Traceback (most recent call last):
   File "C:\Study\KivyMultiScreen\main.py", line 53, in <module>
     CamApp().run()
   File "C:\Study\KivyMultiScreen\main.py", line 47, in build
     sm.add_widget(emotionRecog(name='emotion'))
   File "C:\Study\KivyMultiScreen\main.py", line 35, in __init__
     self.button1.bind(on_press=self.screenTransition())
   File "C:\Study\KivyMultiScreen\main.py", line 41, in screenTransition
     self.manager.current = 'test'
 AttributeError: 'NoneType' object has no attribute 'current'

Process finished with exit code 1

Can you please help me out?你能帮帮我吗?

You are supposed to bind the callback itself to the on_press action.您应该将回调本身绑定到on_press操作。 This way, Kivy can call that function later.这样,Kivy 可以稍后调用该函数。

Change改变

        self.button1.bind(on_press=self.screenTransition())

to

        self.button1.bind(on_press=self.screenTransition)

The reason your script raised that specific error is because Screen.manager is not yet set at the point you called self.screenTransition in that line.您的脚本引发该特定错误的原因是Screen.manager尚未设置在您在该行中调用self.screenTransition的位置。

You can also check out the first several examples in the Kivy documentation .您还可以查看Kivy 文档中的前几个示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM