Kivy add widget won't appear on screen:

Viewed 262

I'm stuck with the following problem:

When I run the following code - this seems to work:

class Board(GridLayout):
    def __init__(self, numLines=8, numCols=8, **kwargs):
        # constructor of the board
        GridLayout._init_(self, **kwargs)
        self.finish_game = Button()
        # Code that operates on the button
        self.finish_game.text = "You lose"
        self.add_widget(self.finish_game)
        # The rest of the code that doesn't matter for now ...

class TestApp(App):
    def build(self):
        self.title = 'based graphics'
        return Board()


TestApp().run()

But when I try this, I see through debugging mode that it goes inside the Board(), but it doesn't show anything on screen:

class StartBoard(Layout):
    def _init_(self):

        Layout._init_(self)
        # Some code that works and not important
        return Board()

class Board(GridLayout): 
  .... #As before

class TestApp(App):
    def build(self):
        self.title = 'based graphics'
        return StartBoard()

TestApp().run()

I know it's not a full code, but maybe you could explain how does TestApp().run() works and why would Board() shows widgets when it run from TestApp.build() and not from StartBoard().

1 Answers

Running Board() creates an instance of the Board class, but that doesn't fundamentally draw anything. It will appear on your screen only if you add it to your widget tree somehow.

In your code you return Board(), but that return doesn't go anywhere so the Board() is instantiated and immediately discarded.

You probably want something like self.add_widget(Board()) instead.

Related