1

I'm currently doing a school project and I can't use .kv files; so I have to do it through normal python. So, I'm converting my previous .kv file, but I'm unsure on how to create multiple screens and add buttons in normal python. Any help is greatly appreciated.

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout

#App windows
class MainMenu(Screen, Button, BoxLayout):
    # def __init__(self, **kwargs):
    #     super(MainMenu, self).__init__(**kwargs)

    menu_s = Screen(name = 'main')

    layout = BoxLayout(orientaion = 'vertical')
    slider_mode = Button(text = 'Slider Mode')
    code_mode = Button(text = 'Code Mode')
    settings = Button(text = 'Settings')

    menu_s.add_widget(slider_mode)
    menu_s.add_widget(code_mode)
    menu_s.add_widget(settings)

class SliderMode(Screen):
    slider_s = Screen(name = 'slider_m')

    layout = BoxLayout(orientaion = 'vertical')
    menu = Button(text = 'Menu')
    slider_s.add_widget(menu)

WindowManager = ScreenManager()
WindowManager.add_widget(MainMenu())
WindowManager.add_widget(SliderMode())

#The app class
class MyMain(App):
    def build(self):
        return WindowManager

#Runs the App
if __name__ == '__main__':
    MyMain().run()

The above code is what I've done so far; but when it's run, it gives this error:

TypeError: object.__init__() takes exactly one argument (the instance to initialize)

This is the .kv file below I'm trying to convert:

WindowManager:
    MainMenu:
    SliderMode:

<MainMenu>:                                             
    name: 'main'                                        
    BoxLayout:                                          
        orientation: 'vertical'
        cols: 1
        Button:                                        
            text: 'Slider Mode'
            size_hint: 0.3, 0.3                         
            pos_hint: {'x':0.35}                        
            on_release:
                app.root.current = 'slider_m'
        Button:                                        
            text: 'Code Mode'
            size_hint: 0.3, 0.3
            pos_hint: {'x':0.35}
            on_release:
                app.root.current = 'code_m'
        Button:                                         
            text: 'Settings'
            size_hint: 0.3, 0.3
            pos_hint: {'x':0.35}
            on_release:
                app.root.current = 'setting_m'

<SliderMode>:
    name: 'slider_m'
    FloatLayout:
        Button:
            text: 'Menu'
            size_hint: 0.15, 0.15
            pos_hint: {'x':0.0, 'y': 0.0}
            on_release:
                app.root.current = 'main'                                  
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Rana24
  • 27
  • 5

1 Answers1

0

It seems that you do not have basic knowledge of OOP since for example you are implementing the inheritance instead of the composition so I recommend you check Difference between Inheritance and Composition, in your case for example MainMenu has a BoxLayout, it is not a BoxLayout.

On the other hand the kv language seems to tolerate errors, which for me is a bug, since BoxLayout does not have a property called cols, instead python is more restrictive so you should not use that variable.

Considering the above, the solution is:

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout


class MainMenu(Screen):
    def __init__(self):
        super(MainMenu, self).__init__(name="main")

        box_lay = BoxLayout(orientation="vertical")
        self.add_widget(box_lay)

        button_1 = Button(
            text="Slider Mode",
            size_hint=(0.3, 0.3),
            pos_hint={"x": 0.35},
            on_release=self.on_released_1,
        )
        button_2 = Button(
            text="Code Mode",
            size_hint=(0.3, 0.3),
            pos_hint={"x": 0.35},
            on_release=self.on_released_2,
        )
        button_3 = Button(
            text="Code Mode",
            size_hint=(0.3, 0.3),
            pos_hint={"x": 0.35},
            on_release=self.on_released_3,
        )

        box_lay.add_widget(button_1)
        box_lay.add_widget(button_2)
        box_lay.add_widget(button_3)

    def on_released_1(self, *args):
        App.get_running_app().root.current = "slider_m"
        # or
        # self.manager.current = "slider_m"

    def on_released_2(self, *args):
        App.get_running_app().root.current = "code_m"
        # or
        # self.manager.current = "code_m"

    def on_released_3(self, *args):
        App.get_running_app().root.current = "setting_m"
        # or
        # self.manager.current = "setting_m"


class SliderMode(Screen):
    def __init__(self):
        super(SliderMode, self).__init__(name="slider_m")

        float_lay = FloatLayout()
        self.add_widget(float_lay)

        button = Button(
            text="Menu",
            size_hint=(0.15, 0.15),
            pos_hint={"x": 0.0, "y": 0.0},
            on_release=self.on_released,
        )
        float_lay.add_widget(button)

    def on_released(self, *args):
        App.get_running_app().root.current = "main"
        # or
        # self.manager.current = "main"


manager = ScreenManager()
manager.add_widget(MainMenu())
manager.add_widget(SliderMode())

# The app class
class MyMain(App):
    def build(self):
        return manager


# Runs the App
if __name__ == "__main__":
    MyMain().run()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241