Kivy 似乎想让所有选项卡具有相同的大小。 如果我想让一个选项卡比其他选项卡更宽怎么办?调整 TabbedPanelItem 的 tab_width 似乎没有效果,因此较长的文本被截断。
示例从 Kivy TabbedPanel docs 稍作修改,我将第一个选项卡的标题更改为“选项卡的长文本”:
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()
...因此“长文本”会被切碎,因为所有 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()
关于python - Kivy (Python) TabbedPanel - 不同(动态)大小的选项卡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48717727/