Good Afternoon all thanks for any insight....again!
How do I call the def clearwidgets
from def changetxt
within an instance, if I use self.parent.clearwidgets
, I receive anchor layout does not support this, which would be correct, as the ultimate parent instance is named root
or GameApp
?
Which is the parent above the parent. If that make sense.
What I'm trying to achieve is like a screen manager/controller to swap from one layout to another: start menu, main play area, end game, credits etc.
I’ve looked at Kivy source code on Github for the demo game but they just go straight to the play area with no splash screen etc.
I even tried adding root=anchorlayout
just under the imports and using global root
in the changetxt()
but receive the error global none not found.
Again Thanks and Appreciated
import kivy
from kivy.app import App
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.stacklayout import StackLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.lang import Builder
class launchScreenMenu(FloatLayout):
def __init__(self, **kwargs):
super(launchScreenMenu, self).__init__(**kwargs)
menuanchor = AnchorLayout(anchor_x='left',anchor_y='bottom')
menu = StackLayout(orientation='bt-lr',size_hint=(0.5,1))
about_btn = Button(text='About',size_hint=(0.3,0.1))
help_btn = Button(text='Settings',size_hint=(0.3,0.1))
settings_btn = Button(text='Help',size_hint=(0.3,0.1))
menu.add_widget(about_btn)
menu.add_widget(help_btn)
menu.add_widget(settings_btn)
menuanchor.add_widget(menu)
return self.add_widget(menuanchor)
class launchScreenBtn(AnchorLayout):
def __init__(self, **kwargs):
super(launchScreenBtn, self).__init__(**kwargs)
play_btn = Button(text="Play")
self.anchor_x = 'center'
self.anchor_y = 'center'
self.size_hint = 0.2,0.2
self.add_widget(play_btn)
play_btn.bind(on_press=self.changetxt)
def changetxt(self,play_btn):
play_btn.text = 'Game Over'
clearwidgets()
class GameApp(App):
def build(self):
root = AnchorLayout()
root.add_widget(launchScreenMenu())
root.add_widget(launchScreenBtn())
return root
def clearwidgets(self):
root.clear_widgets()
if __name__=='__main__':
GameApp().run()