处理2个.kv文件的多个.kv文件

时间:2019-03-03 15:11:43

标签: python python-3.x kivy screen

大家好,我是我在这里提出的最后一个问题,对不起,由于这个问题使我无法进入决赛,所以我没有完成比赛,所以在这里,我有一个带有2个“开始”按钮“ START1”和“ START2”的菜单“所以我想要的是每当我按“ START1”将其带到指定的.kv文件,当我按“ START2”将其带到指定的.kv文件,这是我想要的图片和我完成的示例到目前为止enter image description here

#: import FadeTransition kivy.uix.screenmanager.FadeTransition


ScreenManagement:

    transition: FadeTransition()
    MainScreen:
    GameScreen1:
    GameScreen2:


  Button:

    size_hint: 0.2,0.1
    font_size: 20


  MainScreen:

    name: "Start"

    Button:

        on_release: app.root.current = "game1"
        text: "START"
        color: 1,0,0,1

GameScreen1:

    name: "game1"

    Button:

        pos_hint: {"right":1,"top":0.5}
        color: 1,0,0,1 
        on_release: app.root.current = "game2"
        text: "Next"
        background: 0,1,0,1

    Label: 

        pos_hint: {"x": 0, "y": 0}
        text: "Python\n\n Ruby"
        font_size:40

    Button:

        text: "HOME"
        on_release: app.root.current = "Start"
        pos_hint: {"x": 0.1, "y": 0}


GameScreen2:

    name: "game2"

    Button:

        pos_hint: {"right":1,"top":0.5}
        color: 1,0,0,1 
        on_release: app.root.current = "game1"
        text: "Next"
        background: 0,1,0,1

    Label: 

        pos_hint: {"x": 0, "y": 0}
        text: "Assembly\n\n Prolog"
        font_size:40

    Button:

        text: "HOME"
        on_release: app.root.current = "Start"
        pos_hint: {"x": 0.1, "y": 0}

那是程序员。kv

#: import FadeTransition kivy.uix.screenmanager.FadeTransition


ScreenManagement:
    transition: FadeTransition()
    MainScreen:
    GameScreen1:
    GameScreen2:


Button:

    size_hint: 0.2,0.1
    font_size: 20


MainScreen:

    name: "Start"

    Button:

        on_release: app.root.current = "game1"
        text: "START"
        color: 1,0,0,1

GameScreen1:

    name: "game1"

    Button:

        pos_hint: {"right":1,"top":0.5}
        color: 1,0,0,1 
        on_release: app.root.current = "game2"
        text: "Next"
        background: 0,1,0,1

    Label: 

        pos_hint: {"x": 0, "y": 0}
        text: "HELLO"
        font_size:40

    Button:

        text: "HOME"
        on_release: app.root.current = "Start"
        pos_hint: {"x": 0.1, "y": 0}


GameScreen2:

    name: "game2"

    Button:

        pos_hint: {"right":1,"top":0.5}
        color: 1,0,0,1 
        on_release: app.root.current = "game1"
        text: "Next"
        background: 0,1,0,1

    Label: 

        pos_hint: {"x": 0, "y": 0}
        text: "WORLD"
        font_size:40

    Button:

        text: "HOME"
        on_release: app.root.current = "Start"
        pos_hint: {"x": 0.1, "y": 0}

那是social.kv

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.uix.label import Label



class MainScreen(Screen):

    pass

class GameScreen1(Screen):

    pass

class GameScreen2(Screen):

    pass


class ScreenManagement(ScreenManager):

    pass

presentation = Builder.load_file("social.kv")
presentation = Buider.load_file("programmer.kv")

class Myapp(App):

    def builder(self):

        return presentation

if __name__ == "__main__":
    Myapp().run()

我试图制造2个建造者,但是由于两个建造者都覆盖了另一个“菜单”屏幕,我想我可以这样说。因此,我只想拥有一个带有2个开始按钮的菜单,如上图所示,这将使一个开始来激活指定的.kv文件,任何人都可以提供帮助,现在我不知道如何执行此操作,仍然会重新确定。 / p>

1 个答案:

答案 0 :(得分:0)

这里是如何执行您所要求的示例。使用2个kv文件。 ScreenManager会为您执行此操作。

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout

presentation1 = Builder.load_string('''
Button:
    text: 'This is presentation1'
    on_press: app.switchToRoot2()
''')

presentation2 = Builder.load_string('''
Button:
    text: 'This is presentation2'
    on_press: app.switchToRoot1()
''')

class MultiplKVApp(App):
    def build(self):
        self.root_layout = FloatLayout()
        self.root_layout.add_widget(presentation1)
        return self.root_layout

    def switchToRoot2(self, *args):
        self.root_layout.clear_widgets()
        self.root_layout.add_widget(presentation2)

    def switchToRoot1(self, *args):
        self.root_layout.clear_widgets()
        self.root_layout.add_widget(presentation1)

MultiplKVApp().run()

请注意,您的两个kv文件都具有MainScreenGameScreen1GameScreen2的实例。因此,kivy将为每个小部件创建两个独立的实例。