我有使用Python代码动态构建的TabbedPanelItem。见下面的代码。我有3个问题:
Python代码:
from kivy.lang import Builder
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import ObjectProperty
import string
class RootWidget(FloatLayout):
tab_panel = ObjectProperty(None)
tab_1 = ObjectProperty(None)
def init_root(self):
panel = self.ids.tab_panel
tpitem = string.Template('''
TabbedPanelItem:
id: '$tab_id'
background_normal: '$imgn'
background_down: '$imgd'
BoxLayout:
Label:
text: '$label_txt'
font_size: 14
color: 1,0,0,1
''')
for idx in range (5, 0, -1):
event = str(idx)
tab_id = 'tab_' + event
imgn = './images/tn-' + event + '.png'
imgd = './images/td-' + event + '.png'
label_txt = tab_id + " intentionally left blank"
next_tab = tpitem.substitute(vars())
tab = Builder.load_string(next_tab)
panel.add_widget(tab)
#self.tab_panel.switch_to(self.tab_1)
class addtabApp(App):
def build(self):
homeWin = RootWidget()
homeWin.init_root()
return homeWin
if __name__ == '__main__':
addtabApp().run()
kv文件:
<RootWidget>:
tab_panel:tab_panel
#tab_1:tab_1
TabbedPanel:
id: tab_panel
do_default_tab: False
tab_pos: 'left_top'
答案 0 :(得分:0)
1)default_tab - 这个人想要对象,所以使用id
或<class>.ids.<tab id>
在kv中获取一个,以便您可以在属性中使用它。或者甚至通过<class>.children
- 最后一项应该是第一个标签。例如:
TabbedPanel:
default_tab: deflt
TabbedPanelItem:
text: '1'
Button:
TabbedPanelItem:
text: '2'
TabbedPanelItem:
id: deflt
text: '3'
Label:
text: 'the default one'
2)switch_to - 一个想要切换的对象的函数。再次使用id
或children
your_tab_panel.switch_to(<some id from kv or object>)