1

I am using a screen manager and would like to add widgets to the screen subclass without using the .kv file.

class MainMenu(Screen):

    def __init__(self, **kwargs):
        gLayout = GridLayout()
        gLayout.add_widget(Button(text = 'test'))

class Sis(App):
     def build(self):
         root = ScreenManager()
         root.add_widget(MainMenu(name = 'mainMenu'))
         root.current = 'mainMenu'

         return root

 Sis().run()

When I try to run the above code I get (pygame parachute) Segmentation Fault.

If I create the layout in the .kv file it works fine.

I've tried fiddling around with on_pre_enter and on_enter but I'm pretty sure I was using them wrong.

Any help is appreciated.

1 Answer 1

3

You forgot calling parent constructor of MainMenu class:

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button

class MainMenu(Screen):
    def __init__(self, **kwargs):
        super(MainMenu, self).__init__(**kwargs)
        self.add_widget(Button(text = 'test'))      


class Sis(App):
     def build(self):
         root = ScreenManager()
         root.add_widget(MainMenu(name = 'mainMenu'))
         root.current = 'mainMenu'
         return root

Sis().run()
Sign up to request clarification or add additional context in comments.

1 Comment

That sorted it. I'm off to read up on super. Thanks for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.