Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switching between kivy classes inside one screen

Tags:

python

kivy

I'm looking for a way to change a part of a screen between 'DownPanel1' and 'DownPanel1' but I would like to avoide creating nex screen class 'ToAvoid'. Is it possible?

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()
like image 694
ForyszeP Avatar asked Jun 09 '26 04:06

ForyszeP


1 Answers

How about some inception? Just put another ScreenManager inside of the other.
Try this example:

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()
like image 98
el3ien Avatar answered Jun 10 '26 19:06

el3ien