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:
JavaScript
x
27
27
1
from kivy.app import App
2
from kivy.uix.screenmanager import ScreenManager, Screen
3
from kivy.uix.tabbedpanel import TabbedPanel
4
5
class TabbedTest(TabbedPanel):
6
pass
7
class MainScreen(Screen):
8
pass
9
class Firstscreen(Screen):
10
pass
11
class Secondscreen(Screen):
12
pass
13
14
class myapp(App):
15
16
def build(self):
17
# Create the screen manager
18
sm = ScreenManager()
19
sm.add_widget(MainScreen(name='main'))
20
sm.add_widget(Firstscreen(name='first'))
21
sm.add_widget(Secondscreen(name='second'))
22
23
return sm
24
25
if __name__ == '__main__':
26
myapp().run()
27
kv:
JavaScript
1
30
30
1
<TabbedTest>:
2
do_default_tab: False
3
TabbedPanelItem:
4
text:"test"
5
6
<MainScreen>:
7
BoxLayout:
8
Button:
9
text: 'first'
10
on_press: root.manager.current = 'first'
11
Button:
12
text: 'second'
13
on_press: root.manager.current = 'second'
14
15
<Firstscreen>:
16
BoxLayout:
17
Label:
18
text:"first"
19
Button:
20
text: 'Back to main'
21
on_press: root.manager.current = 'main'
22
23
<Secondscreen>:
24
BoxLayout:
25
Label:
26
text:'second'
27
Button:
28
text: 'Back to main'
29
on_press: root.manager.current = 'main'
30
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:
JavaScript
1
14
14
1
def build(self):
2
root = FloatLayout()
3
self.tabbedtest = TabbedTest(size_hint_y=0.5, pos_hint={'top': 1.0}) # later you can use self.tabbedtest to add more tabs
4
root.add_widget(self.tabbedtest)
5
6
# Create the screen manager
7
sm = ScreenManager(size_hint_y=0.5, pos_hint={'y': 0})
8
sm.add_widget(MainScreen(name='main'))
9
sm.add_widget(Firstscreen(name='first'))
10
sm.add_widget(Secondscreen(name='second'))
11
root.add_widget(sm)
12
13
return root
14