Skip to content
Advertisement

kivy: Is it possible to have the same Tabbed Panel on different screens?

im wondering if it is possible to have a Tabbed Panel permanently even though im switching between screens? Ive tried having the TabbedPanel outside the mainscreen, on a different class and so on. So far when i switch to the FirstScreen the TabbedPanel disapears.

Python:

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen 
from kivy.uix.tabbedpanel import TabbedPanel

class TabbedTest(TabbedPanel):
    pass
class MainScreen(Screen):
    pass
class Firstscreen(Screen):
    pass
class Secondscreen(Screen):
    pass

class myapp(App):

def build(self):
    # Create the screen manager
    sm = ScreenManager()
    sm.add_widget(MainScreen(name='main'))
    sm.add_widget(Firstscreen(name='first'))
    sm.add_widget(Secondscreen(name='second'))

    return sm

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

kv:

<TabbedTest>:
    do_default_tab: False
    TabbedPanelItem:
        text:"test"

<MainScreen>:
    BoxLayout:
        Button:
            text: 'first'
            on_press: root.manager.current = 'first'
        Button:
            text: 'second'
            on_press: root.manager.current = 'second'

<Firstscreen>:
    BoxLayout:
        Label:
            text:"first"
        Button:
            text: 'Back to main'
            on_press: root.manager.current = 'main'

<Secondscreen>:
    BoxLayout:
        Label:
            text:'second'
        Button:
            text: 'Back to main'
            on_press: root.manager.current = 'main'

Advertisement

Answer

Rather than adding the TabbedTest to each Screen, just make the TabbedTest and ScreenManager as children of the root widget of the App. One way to do this is to modify the build() method:

def build(self):
    root = FloatLayout()
    self.tabbedtest = TabbedTest(size_hint_y=0.5, pos_hint={'top': 1.0})  # later you can use self.tabbedtest to add more tabs
    root.add_widget(self.tabbedtest)

    # Create the screen manager
    sm = ScreenManager(size_hint_y=0.5, pos_hint={'y': 0})
    sm.add_widget(MainScreen(name='main'))
    sm.add_widget(Firstscreen(name='first'))
    sm.add_widget(Secondscreen(name='second'))
    root.add_widget(sm)

    return root
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement