如何创建多个屏幕并通过python

时间:2019-09-17 22:57:52

标签: python kivy kivy-language

我目前正在做一个学校项目,不能使用.kv文件;所以我必须通过普通的python来做。因此,我正在转换以前的.kv文件,但是不确定如何在普通的python中创建多个屏幕和添加按钮。任何帮助将不胜感激。

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()

以上代码是我到目前为止所做的;但是运行时会出现此错误:

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

这是我要转换的以下.kv文件:

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'                                  

1 个答案:

答案 0 :(得分:0)

您似乎没有OOP的基本知识,因为例如您正在实现继承而不是组成,所以我建议您检查Difference between Inheritance and Composition,例如您的MainMenu BoxLayout,它不是

另一方面,kv语言似乎可以容忍错误,对我来说这是个错误,因为BoxLayout没有名为cols的属性,而python更具限制性,因此您不应使用该变量。

考虑到上述情况,解决方案是:

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()