所以我正在做一个 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'
如果有人告诉我为什么这不起作用以及如何解决这个问题/像我想要的那样进行转换,我会很高兴。