Kivy:如何定义在打开TabbedPanel时要激活哪个Tab
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Kivy:如何定义在打开TabbedPanel时要激活哪个Tab相关的知识,希望对你有一定的参考价值。
如何在打开TabbedPanel时定义哪个选项卡处于活动状态?
这里我在左侧使用选项卡,因此希望选项卡#3在开始时处于活动状态,而不是选项卡#1。
import kivy, os
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
from kivy.base import runTouchApp
Builder.load_string("""
<TabbedTestScreen>:
TabbedPanel:
do_default_tab: False
tab_pos: 'left_top'
tab_height: 90
tab_width: 90
TabbedPanelItem:
text: '1'
Label:
text: '1'
TabbedPanelItem:
text: '2'
Label:
text: '2'
TabbedPanelItem:
text: '3'
id: home_tab
Label:
text: '3'
""")
class TabbedTestScreen(Screen):
pass
runTouchApp(TabbedTestScreen())
答案
您可以在类初始化时使用switch_to
执行此操作:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
from kivy.base import runTouchApp
from kivy.properties import ObjectProperty
from kivy.clock import Clock
Builder.load_string("""
<TabbedTestScreen>:
tab_panel: tab_panel
home_tab: home_tab
TabbedPanel:
id: tab_panel
do_default_tab: False
tab_pos: 'left_top'
tab_height: 90
tab_width: 90
TabbedPanelItem:
text: '1'
Label:
text: '1'
TabbedPanelItem:
text: '2'
Label:
text: '2'
TabbedPanelItem:
text: '3'
id: home_tab
Label:
text: '3'
""")
class TabbedTestScreen(Screen):
tab_panel = ObjectProperty(None)
home_tab = ObjectProperty(None)
def __init__(self, **kwargs):
super(TabbedTestScreen, self).__init__(**kwargs)
Clock.schedule_once(lambda *args: self.tab_panel.switch_to(self.home_tab))
runTouchApp(TabbedTestScreen())
以上是关于Kivy:如何定义在打开TabbedPanel时要激活哪个Tab的主要内容,如果未能解决你的问题,请参考以下文章
在 TabbedPanel 中获取小部件/项目 ID [关闭]