更改 kivy window 过渡方向会在不同 class 中尝试时引发错误?

Changing the kivy window transition direction pulls up an error when tried in different class?

所以我正在做一个 kivy 练习应用程序,我正在用 WindowManager 改变 windows。我们可以选择改变过渡的方向,它在某个地方完美地工作,但在某个地方不完美,我不完全理解为什么。我想解决这个问题。

这是我的主要程序:

import kivy
from kivy.uix.widget import Widget
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.scrollview import ScrollView
from kivy.metrics import dp
from kivy.uix.stacklayout import StackLayout
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen

class MainWindow(Screen):
    pass
class SecondWindow(Screen):
    pass
class WindowManager(ScreenManager):
    pass


class ScrollScreen(ScrollView):
    pass

class StackLayoutExample(StackLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        # for i in range(0, 400):
            #size = dp(100) + i*10
            #size = dp(100)
            #b = Button(text=str(i+1), size_hint=(None, None), size=(size, size))
            #self.add_widget(b)

class MyApp(App):
    pass

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

这是我的 .kv 文件:

WindowManager:
    MainWindow:
    SecondWindow:


<MainWindow>:
    name: 'main'
    ScrollScreen:
<SecondWindow>:
    name: 'second'
    Label:
        text: 'this is the second screen'
    Button:
        text: 'go back'
        on_release:
            root.manager.transition.direction = 'left'
            app.root.current = 'main'
            


<ScrollScreen@ScrollView>:
    do_scroll_x: False
    do_scroll_y: True

    StackLayoutExample:
        size_hint: 1, None
        height: 1000

<StackLayoutExample>:
    Button:
        text:'Press me!'
        size_hint: None, None
        pos_hint: {'center_x': .5, 'top': 1}
        width: "120dp"
        height: "60dp"
        on_release:
            root.manager.transition.direction = 'right'
            app.root.current = 'second'

这是我在 Main window 中按下 Press me 按钮时得到的回溯:

Traceback (most recent call last):
   File "c:\Users\aatut\Documents\Code\PythonProjektit\kivytraining\firstpractise\main.py", line 36, in <module>
     MyApp().run()
   File "C:\Users\aatut\Documents\Code\lib\site-packages\kivy\app.py", line 950, in run
     runTouchApp()
   File "C:\Users\aatut\Documents\Code\lib\site-packages\kivy\base.py", line 582, in runTouchApp
     EventLoop.mainloop()
   File "C:\Users\aatut\Documents\Code\lib\site-packages\kivy\base.py", line 347, in mainloop
     self.idle()
   File "C:\Users\aatut\Documents\Code\lib\site-packages\kivy\base.py", line 391, in idle
     self.dispatch_input()
   File "C:\Users\aatut\Documents\Code\lib\site-packages\kivy\base.py", line 342, in dispatch_input
     post_dispatch_input(*pop(0))
   File "C:\Users\aatut\Documents\Code\lib\site-packages\kivy\base.py", line 308, in post_dispatch_input
     wid.dispatch('on_touch_up', me)
   File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
   File "C:\Users\aatut\Documents\Code\lib\site-packages\kivy\uix\behaviors\button.py", line 179, in on_touch_up
     self.dispatch('on_release')
   File "kivy\_event.pyx", line 705, in kivy._event.EventDispatcher.dispatch
   File "kivy\_event.pyx", line 1248, in kivy._event.EventObservers.dispatch
   File "kivy\_event.pyx", line 1132, in kivy._event.EventObservers._dispatch
   File "C:\Users\aatut\Documents\Code\lib\site-packages\kivy\lang\builder.py", line 57, in custom_callback
     exec(__kvlang__.co_value, idmap)
   File "c:\Users\aatut\Documents\Code\PythonProjektit\kivytraining\firstpractise\my.kv", line 37, in <module>
     root.manager.transition.direction = 'right'
   File "kivy\weakproxy.pyx", line 32, in kivy.weakproxy.WeakProxy.__getattr__
 AttributeError: 'StackLayoutExample' object has no attribute 'manager'

如果有人告诉我为什么这不起作用以及如何修复 this/do 转换,我会很高兴。

错误消息告诉您 StackLayoutExample 没有 manager 属性,因为在您的代码中:

root.manager.transition.direction = 'right'

root指的是它出现的规则的根,即StackLayoutExample。您想要访问 WindowManager,就像您在设置 current Screen 的行中所做的那样。所以你的代码应该是:

    on_release:
        app.root.transition.direction = 'right'
        app.root.current = 'second'

请注意,当您引用 app 时,您引用的是当前 运行 Appapp.root 指的是 App 的根控件。正如 object.attribute.

形式的任何引用一样

但是,当您使用 root 开始引用时,您引用的是使用该关键字的规则的根对象。相关documentation.