我要做的是创建两个屏幕,每个屏幕有两个按钮。一个按钮切换到另一个屏幕,另一个按钮只执行一些代码(现在只是一个打印语句)。我无法弄清楚我应该如何将一个名字绑在屏幕上,因为当我尝试用我的按钮切换屏幕时我得到了错误
kivy.uix.screenmanager.ScreenManagerException:没有名称为“screen2”的屏幕
我尝试打印出我创建的屏幕的名称,然后我就
了['','']
所以我假设我制作的两个屏幕已经创建,但我尝试命名它们失败了。
这是我的完整代码:
#PYTHON 3.4.4
#KIVY 1.9.1
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.screenmanager import ScreenManager, Screen
class ScreenOne(Screen):
def __init__(self, **kwargs):
super().__init__()
btn1 = Button(
text='change screen',
size_hint=(.5, .25),
pos_hint={'left':0, 'top':1}
)
btn1.bind(on_press=self.changer)
self.add_widget(btn1)
other_btn1 = Button(
text='test button',
size_hint=(.5, .25),
pos_hint={'right':1, 'bottom':0}
)
other_btn1.bind(on_press=self.test)
self.add_widget(other_btn1)
def changer(self,*args):
self.manager.current = 'screen2'
def test(self,instance):
print('This is a test')
class ScreenTwo(Screen):
def __init__(self, **kwargs):
super().__init__()
btn2 = Button(
text='change screen',
size_hint=(.5, .25),
pos_hint={'left':0, 'top':1}
)
btn2.bind(on_press=self.changer)
self.add_widget(btn2)
other_btn2 = Button(
text='test button 2',
size_hint=(.5, .25),
pos_hint={'right':1, 'bottom':0}
)
other_btn2.bind(on_press=self.test)
self.add_widget(other_btn2)
def changer(self,*args):
self.manager.current = 'screen1'
def test(self,instance):
print('This is another test')
class TestApp(App):
def build(self):
sm = ScreenManager()
sc1 = ScreenOne(name='screen1')
sc2 = ScreenTwo(name='screen2')
sm.add_widget(sc1)
sm.add_widget(sc2)
print (sm.screen_names)
return sm
if __name__ == '__main__':
TestApp().run()
答案 0 :(得分:1)
如果你想要你的孩子"作为父母"给他一个机会听同样的" kwargs"。
使用** kwargs输入super()。 init (),它将起作用。