在不同的类中尝试时,更改 kivy 窗口转换方向会引发错误吗?

所以我正在做一个 kivy 练习应用程序,我正在使用 WindowManager 更改窗口。我们有一个选项来改变过渡的方向,它在某处完美运行,但在某处却没有,我不完全明白为什么。我想解决这个问题。

这是我的主程序:

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'

这是我在主窗口中按下 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'

如果有人告诉我为什么这不起作用以及如何解决这个问题/像我想要的那样进行转换,我会很高兴。

chenyaleiBB 回答:在不同的类中尝试时,更改 kivy 窗口转换方向会引发错误吗?

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

root.manager.transition.direction = 'right'

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

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

请注意,当您引用 app 时,您是在引用当前运行的 App。而 app.root 指的是 App 的根小部件。就像形式 object.attribute 的任何引用一样。

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

本文链接:https://www.f2er.com/2433.html

大家都在问