Kivy (Python) TabbedPanel - 不同(动态)大小的标签?
Kivy (Python) TabbedPanel - different (dynamic) sized tabs?
Kivy 似乎想让所有的标签都一样大。
如果我想让一个选项卡比其他选项卡更宽怎么办?为 TabbedPanelItem 调整 tab_width 似乎没有效果,因此较长的文本被截断。
示例从 Kivy TabbedPanel docs 稍作修改,我将第一个选项卡的标题更改为 "Long Text for a Tab":
from kivy.app import App
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.lang import Builder
Builder.load_string("""
<Test>:
#size_hint: .5, .5
pos_hint: {'center_x': .5, 'center_y': .5}
do_default_tab: False
TabbedPanelItem:
text: 'Long Text for a Tab'
tab_width: self.texture_size[0]
Label:
text: 'First tab content area'
TabbedPanelItem:
text: 'tab2'
BoxLayout:
Label:
text: 'Second tab content area'
Button:
text: 'Button that does nothing'
TabbedPanelItem:
text: 'tab3'
RstDocument:
text:
'\n'.join(("Hello world", "-----------",
"You are in the third tab."))
""")
class Test(TabbedPanel):
pass
class TabbedPanelApp(App):
def build(self):
return Test()
if __name__ == '__main__':
TabbedPanelApp().run()
...所以 "Long Text" 被切碎了,因为所有 3 个选项卡的宽度都相同。即使将第一个选项卡的 tab_width 设置为常量(例如 300)也会被忽略。
似乎您可以向 TabbedPanel 提供 tab_width 并且它使所有选项卡都相同,但是如何使各个选项卡 (TabbedPanelItems) 具有不同的(甚至是动态的)宽度,即一些选项卡比别人宽?
tab_width
是 TabbedPanel
属性,而不是 TabbedPanelItem
属性,并且应用于所有选项卡。
要为每个选项卡设置不同的大小,您可以将 tab_widht
设置为 None
,然后正确使用 TabbedPanelItem
属性(它基于 Togglebutton
,因此您可以使用 size_hint_x
和 size
属性):
<Test>:
pos_hint: {'center_x': .5, 'center_y': .5}
do_default_tab: False
tab_width: None
TabbedPanelItem:
text: 'Long Text for a Tab'
size_hint_x: None
width: self.texture_size[0]
padding: 10, 0
Label:
text: 'First tab content area'
编辑:
显然有必要在 TabbedPanel
初始化后显式更新选项卡的宽度,您可以使用 Clock.schedule
_once 和 on_tab_width
方法来执行此操作:
from kivy.app import App
from kivy.uix.tabbedpanel import TabbedPanel, TabbedPanelItem
from kivy.uix.boxlayout import BoxLayout
from kivy.clock import Clock
from kivy.lang import Builder
Builder.load_string("""
<CustomWidthTabb@TabbedPanelItem>
width: self.texture_size[0]
padding: 10, 0
size_hint_x: None
<Test>:
size_hint: 1,1
do_default_tab: False
tab_width: None
CustomWidthTabb:
text: "This is a Long Tab"
Label:
text: 'First tab content area'
CustomWidthTabb:
text: "This is a Long Tab"
Label:
text: 'Second tab content area'
CustomWidthTabb:
text: "Short Tab"
Label:
text: 'Third tab content area'
CustomWidthTabb:
text: "Short Tab#2"
Label:
text: 'Fourth tab content area'
""")
class Test(TabbedPanel):
def __init__(self, *args, **kwargs):
super(Test, self).__init__(*args, **kwargs)
Clock.schedule_once(self.on_tab_width, 0.1)
class TabbedPanelApp(App):
def build(self):
return Test()
if __name__ == '__main__':
TabbedPanelApp().run()
Kivy 似乎想让所有的标签都一样大。 如果我想让一个选项卡比其他选项卡更宽怎么办?为 TabbedPanelItem 调整 tab_width 似乎没有效果,因此较长的文本被截断。
示例从 Kivy TabbedPanel docs 稍作修改,我将第一个选项卡的标题更改为 "Long Text for a Tab":
from kivy.app import App
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.lang import Builder
Builder.load_string("""
<Test>:
#size_hint: .5, .5
pos_hint: {'center_x': .5, 'center_y': .5}
do_default_tab: False
TabbedPanelItem:
text: 'Long Text for a Tab'
tab_width: self.texture_size[0]
Label:
text: 'First tab content area'
TabbedPanelItem:
text: 'tab2'
BoxLayout:
Label:
text: 'Second tab content area'
Button:
text: 'Button that does nothing'
TabbedPanelItem:
text: 'tab3'
RstDocument:
text:
'\n'.join(("Hello world", "-----------",
"You are in the third tab."))
""")
class Test(TabbedPanel):
pass
class TabbedPanelApp(App):
def build(self):
return Test()
if __name__ == '__main__':
TabbedPanelApp().run()
...所以 "Long Text" 被切碎了,因为所有 3 个选项卡的宽度都相同。即使将第一个选项卡的 tab_width 设置为常量(例如 300)也会被忽略。
似乎您可以向 TabbedPanel 提供 tab_width 并且它使所有选项卡都相同,但是如何使各个选项卡 (TabbedPanelItems) 具有不同的(甚至是动态的)宽度,即一些选项卡比别人宽?
tab_width
是 TabbedPanel
属性,而不是 TabbedPanelItem
属性,并且应用于所有选项卡。
要为每个选项卡设置不同的大小,您可以将 tab_widht
设置为 None
,然后正确使用 TabbedPanelItem
属性(它基于 Togglebutton
,因此您可以使用 size_hint_x
和 size
属性):
<Test>:
pos_hint: {'center_x': .5, 'center_y': .5}
do_default_tab: False
tab_width: None
TabbedPanelItem:
text: 'Long Text for a Tab'
size_hint_x: None
width: self.texture_size[0]
padding: 10, 0
Label:
text: 'First tab content area'
编辑:
显然有必要在 TabbedPanel
初始化后显式更新选项卡的宽度,您可以使用 Clock.schedule
_once 和 on_tab_width
方法来执行此操作:
from kivy.app import App
from kivy.uix.tabbedpanel import TabbedPanel, TabbedPanelItem
from kivy.uix.boxlayout import BoxLayout
from kivy.clock import Clock
from kivy.lang import Builder
Builder.load_string("""
<CustomWidthTabb@TabbedPanelItem>
width: self.texture_size[0]
padding: 10, 0
size_hint_x: None
<Test>:
size_hint: 1,1
do_default_tab: False
tab_width: None
CustomWidthTabb:
text: "This is a Long Tab"
Label:
text: 'First tab content area'
CustomWidthTabb:
text: "This is a Long Tab"
Label:
text: 'Second tab content area'
CustomWidthTabb:
text: "Short Tab"
Label:
text: 'Third tab content area'
CustomWidthTabb:
text: "Short Tab#2"
Label:
text: 'Fourth tab content area'
""")
class Test(TabbedPanel):
def __init__(self, *args, **kwargs):
super(Test, self).__init__(*args, **kwargs)
Clock.schedule_once(self.on_tab_width, 0.1)
class TabbedPanelApp(App):
def build(self):
return Test()
if __name__ == '__main__':
TabbedPanelApp().run()