Reputation: 53
What I'm trying to do is create two screens, each with two buttons. One button switches to the other screen, and the other button just executes some code (just a print statement right now). I can't quite figure out how I'm supposed to be tying a name to a screen, because when I try and switch screens with my button I get the error
kivy.uix.screenmanager.ScreenManagerException: No Screen with name "screen2"
I tried printing out the name of the screens I created, and I just get
['', '']
So I assume that the two screens I made were created, but my attempt to name them failed.
Here's my full code:
#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()
Upvotes: 0
Views: 672
Reputation: 136
If you want your "kid" to act as a "parent" give him a chance to listen to the same "kwargs".
Feed super().init() with **kwargs and it will work.
Upvotes: 1