susanz susanz - 1 year ago 580
Python Question

Kivy Adding Widget to a screen

This seems to be a silly question. But I have a widget that I want to add to a screen called GameScreen.

This is my Python code:

class WelcomeScreen(Screen):

class BasicScreen(Screen):

class GameScreen(Screen):
parent = Widget()
game = ShootingGame()
Clock.schedule_interval(game.update, 1.0 / 60.0)
# return parent

sm = ScreenManager()

class ShootingApp(App):
def build(self):
return sm

if __name__ == '__main__':

And this is my kivy code:

text: "Learn about haptic illusions"
size_hint: None, None
size: 500, 70
pos: 100, 200
font_size: 30
on_release: app.root.current = "basic"

text: "Play our game"
size_hint: None, None
size: 500, 70
pos: 100, 100
font_size: 30
on_release: app.root.current = "game"

name: "basic"

name: "game"

The error I am getting is this. And I think this is because I already defined a parent for the widget game. However, I need that parent because the game widget uses width and height values of its parent (e.g., self.parent.width). Is there any workaround for this so that the game widget can be nested in a parent and add the parent to the screen?

kivy.uix.widget.WidgetException: Cannot add <Screen name='game'>, it already has a parent <kivy.uix.widget.Widget object at 0x1093dc8d8>

Thanks guys!!

Answer Source

you can do something like this

class GamesScreen(Screen):
    def __init__(self, **kwargs):
        super(GameScreen, self).__init__(**kwargs) = ShootingGame()
        clock.schedule_interval(, 1.0 / 60.0)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download