在一个屏幕内的kivy类之间切换

时间:2019-04-03 04:41:03

标签: python kivy

我正在寻找一种在'DownPanel1''DownPanel1'之间更改屏幕部分的方法,但是我想避免创建nex屏幕类'ToAvoid'。有可能吗?

from kivy.config import Config
Config.set('graphics', 'multisamples', '0')
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.boxlayout import BoxLayout

kv = '''
#:import FadeTransition kivy.uix.screenmanager.FadeTransition

ScreenManagement:
    transition: FadeTransition()
    SomeScreen:
    ToAvoid:

<Menu@RelativeLayout>
    id: main_menu
    size_hint_x: None
    width: 120
    Button:
        size_hint_y: None
        pos: root.x, root.top - self.height
        text: 'SomeScreen'
        on_press: app.root.current = "SomeScreen"

<UpScreen>:
    BoxLayout:
        Button:
            text: 'switch'
            on_press: app.root.current = "ToAvoid"

<DownPanel1>:
    Button:
        text: 'DownPanel1'
        #on_press:

<DownPanel2>:
    Button:
        text: 'DownPanel2'
        #on_press:
<SomeScreen>:
    name: 'SomeScreen'
    BoxLayout:
        orientation: 'horizontal'
        Menu:
        BoxLayout:
            orientation: 'vertical'
            UpScreen:  
            DownPanel1:
<ToAvoid>: 
    name: 'ToAvoid'
    BoxLayout:
        orientation: 'horizontal'
        Menu:
        BoxLayout:
            orientation: 'vertical'
            UpScreen:  
            DownPanel2: 

'''

class DownPanel1(BoxLayout):
    pass

class DownPanel2(BoxLayout):
    pass

class UpScreen(Screen):
    pass

class SomeScreen(Screen):
    pass

class ToAvoid(Screen):
    pass

class ScreenManagement(ScreenManager):
    pass

sm = Builder.load_string(kv)

class TestApp(App):
    def build(self):
        return sm

if __name__ == '__main__':
    TestApp().run()

1 个答案:

答案 0 :(得分:2)

如何开始?只需将另一个ScreenManager放在另一个内部即可。
试试这个例子:

from kivy.app import App
from kivy.lang import Builder


KV = """

ScreenManager:
    Screen:
        name: "1st"
        Button:
            text: "next"
            on_release:
                root.current = "2nd"
    Screen:
        name: "2nd"
        BoxLayout:
            Button:
                text: "3rd to avoid"
                on_release:
                    root.current = "3rd"
            ScreenManager:
                id: sm2
                Screen:
                    name: "inner1"
                    Button:
                        text: "go to inner 2"
                        on_release:
                            sm2.current = "inner2"
                Screen:
                    name: "inner2"
                    Label:
                        text: "This is inner 2!"
    Screen:
        name: "3rd"
        Label:
            text: "To Avoid!"

"""


class MyApp(App):

    def build(self):
        return Builder.load_string(KV)


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