[英]Problems with ScreenManager and Screen in Python (kivy). I want to call on def in Screen from the Screenmanager class
I am trying to call on def in class Screen from Screenmanager from the class Screenmanager but I can't.我正在尝试从 class Screenmanager 的 Screenmanager 调用 class 屏幕中的 def,但我不能。
I want to make a box layout and put a button in that boxlayout when the app is started as soon as ScreenManager is called so it is the default screen.我想在调用 ScreenManager 后立即启动应用程序时制作一个框布局并在该框布局中放置一个按钮,因此它是默认屏幕。 I want to do this so I can switch between multiple screens using the self.parent.current = 'Screen Name' on the press of a button
我想这样做,这样我就可以在按下按钮时使用 self.parent.current = 'Screen Name' 在多个屏幕之间切换
Here is the code这是代码
class Manager(ScreenManager):
def __init__(self):
self.add_widget(Scr1().make_button())
class Scr1(Screen):
def __init__(self):
self.layout = BoxLayout(orientation = 'vertical')
self.button = Button(text='hello')
self.layout.add_widget(self.button)
def make_button(self, layout):
return self.layout
class MainApp(App):
def build(self):
sm =Manager()
s1 = Scr1()
sm.add_widget=s1
return sm
if __name__ == "__main__":
MainApp().run()
Here is the error I get这是我得到的错误
Traceback (most recent call last):
File "C:/Users/user/Desktop/CarsonMusic/Learn.py", line 43, in <module>
MainApp().run()
File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\kivy\app.py", line 829, in run
root = self.build()
File "C:/Users/user/Desktop/CarsonMusic/Learn.py", line 37, in build
sm =Manager()
File "C:/Users/user/Desktop/CarsonMusic/Learn.py", line 24, in __init__
self.add_widget(Scr1().make_button())
File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\kivy\uix\screenmanager.py", line 979, in add_widget
'ScreenManager accepts only Screen widget.')
kivy.uix.screenmanager.ScreenManagerException: ScreenManager accepts only Screen widget.
I know I am probably making a dumb mistake but can someone help me?我知道我可能犯了一个愚蠢的错误,但有人可以帮助我吗?
I solved it, I had to delete the Manager(ScreenManager) class, added the 'super', and then I added the layout widget to self我解决了,我必须删除 Manager(ScreenManager) class,添加“超级”,然后将布局小部件添加到 self
Here is my new code:这是我的新代码:
class Scr1(Screen):
def __init__(self, **kwargs):
super (Scr1, self).__init__(**kwargs)
self.layout = BoxLayout(orientation='vertical')
self.button = Button(text='hello')
self.button.bind(on_press=self.screen2)
self.layout.add_widget(self.button)
self.add_widget(self.layout)
def screen2(self, *args):
self.manager.current = 'screen2'
class Scr2(Screen):
def __init__(self, **kwargs):
super (Scr2,self).__init__(**kwargs)
self.layout2 = BoxLayout(orientation='vertical')
self.add_widget(self.layout2)
self.button2 = Button(text='hello2')
self.button2.bind(on_press=self.screen1)
self.layout2.add_widget(self.button2)
def screen1(self, *args):
self.manager.current = 'screen1'
class MainApp(App):
def build(self):
sm= ScreenManager()
screen1 = Scr1(name='screen1')
screen2= Scr2(name='screen2')
sm.add_widget(screen1)
sm.add_widget(screen2)
return sm
if __name__ == "__main__":
MainApp().run()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.