Python Kivy TabbedPanel - 设置自定义标签宽度
Python Kivy TabbedPanel - setting custom tab widths
感谢多年来在不知不觉中帮助我!!我经常使用这个网站,但这是我第一次真正需要 post 一个问题来获取我需要的信息,这太棒了。
我最近开始学习 Python (& Kivy),所以我还是个初学者。
我正在使用:
[INFO ] [Kivy ] v1.10.0
[INFO ] [Python ] v3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 16:07:46) [MSC v.1900 32 bit (Intel)]
我正在尝试调整 TabbedPanel 中的选项卡 (TabbedPanelItem) 的大小,以便它们具有足够的水平尺寸,以容纳文本。
还有另一个类似的问题 已经用解决方案回答了。
我发现此解决方案也适用于我,但前提是我不使用屏幕并将选项卡式面板放在其他任何东西中。
一旦我更改结构以便嵌套选项卡式面板,解决方案就不再有效。
这是我从解决方案中获取的一些示例代码,但对其进行了修改,以便选项卡式面板不是根:
from kivy.app import App
from kivy.uix.tabbedpanel import TabbedPanel, TabbedPanelItem
from kivy.uix.screenmanager import ScreenManager, Screen, SlideTransition
from kivy.uix.boxlayout import BoxLayout
from kivy.clock import Clock
from kivy.lang import Builder
Builder.load_string("""
<Test2>:
GridLayout:
cols:1
GridLayout:
rows:1
Label:
text:'Does it show all 4 tabs?'
GridLayout:
rows:1
TestForTabbedPanel
<CustomWidthTabb@TabbedPanelItem>
width: self.texture_size[0]
padding: 10, 0
size_hint_x: None
<TestForTabbedPanel>:
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 Test2(Screen):
pass
class TestForTabbedPanel(TabbedPanel):
def __init__(self, *args, **kwargs):
super(TestForTabbedPanel, self).__init__(*args, **kwargs)
Clock.schedule_once(self.on_tab_width, 0.1)
class TabbedPanelApp(App):
def build(self):
return Test2()
if __name__ == '__main__':
TabbedPanelApp().run()
与此相比,另一个线程中提供的不嵌套选项卡面板的解决方案有效;它显示了所有选项卡并且它们已动态调整大小:
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()
如何编写代码,使 TabbedPanel 嵌套在屏幕或其他小部件内,但显示所有选项卡并具有足够的宽度?
我在这里不了解哪些基础知识?
解决方案
详情请参考示例
kv 文件
为 TestForTabbedPanel:
添加 id
GridLayout:
rows:1
TestForTabbedPanel:
id: tp
Python代码
- 将
__init__()
方法从 class TestForTabbedPanel 移动到 class Test2
- 将
super(TestForTabbedPanel, self)
替换为super(Test2, self)
- 将
self.on_tab_width
替换为self.ids.tp.on_tab_width
- 将
pass
添加到 class *TestForTabbedPanel
class Test2(Screen):
def __init__(self, *args, **kwargs):
super(Test2, self).__init__(*args, **kwargs)
Clock.schedule_once(self.ids.tp.on_tab_width, 0.1)
class TestForTabbedPanel(TabbedPanel):
pass
例子
from kivy.app import App
from kivy.uix.tabbedpanel import TabbedPanel, TabbedPanelItem
from kivy.uix.screenmanager import ScreenManager, Screen, SlideTransition
from kivy.uix.boxlayout import BoxLayout
from kivy.clock import Clock
from kivy.lang import Builder
Builder.load_string("""
<Test2>:
GridLayout:
cols:1
GridLayout:
rows:1
Label:
text:'Does it show all 4 tabs?'
GridLayout:
rows:1
TestForTabbedPanel:
id: tp
<CustomWidthTabb@TabbedPanelItem>
width: self.texture_size[0]
padding: 10, 0
size_hint_x: None
<TestForTabbedPanel>:
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 Test2(Screen):
def __init__(self, *args, **kwargs):
super(Test2, self).__init__(*args, **kwargs)
Clock.schedule_once(self.ids.tp.on_tab_width, 0.1)
class TestForTabbedPanel(TabbedPanel):
pass
class TabbedPanelApp(App):
def build(self):
return Test2()
if __name__ == '__main__':
TabbedPanelApp().run()
输出
感谢多年来在不知不觉中帮助我!!我经常使用这个网站,但这是我第一次真正需要 post 一个问题来获取我需要的信息,这太棒了。
我最近开始学习 Python (& Kivy),所以我还是个初学者。 我正在使用:
[INFO ] [Kivy ] v1.10.0
[INFO ] [Python ] v3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 16:07:46) [MSC v.1900 32 bit (Intel)]
我正在尝试调整 TabbedPanel 中的选项卡 (TabbedPanelItem) 的大小,以便它们具有足够的水平尺寸,以容纳文本。
还有另一个类似的问题
一旦我更改结构以便嵌套选项卡式面板,解决方案就不再有效。
这是我从解决方案中获取的一些示例代码,但对其进行了修改,以便选项卡式面板不是根:
from kivy.app import App
from kivy.uix.tabbedpanel import TabbedPanel, TabbedPanelItem
from kivy.uix.screenmanager import ScreenManager, Screen, SlideTransition
from kivy.uix.boxlayout import BoxLayout
from kivy.clock import Clock
from kivy.lang import Builder
Builder.load_string("""
<Test2>:
GridLayout:
cols:1
GridLayout:
rows:1
Label:
text:'Does it show all 4 tabs?'
GridLayout:
rows:1
TestForTabbedPanel
<CustomWidthTabb@TabbedPanelItem>
width: self.texture_size[0]
padding: 10, 0
size_hint_x: None
<TestForTabbedPanel>:
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 Test2(Screen):
pass
class TestForTabbedPanel(TabbedPanel):
def __init__(self, *args, **kwargs):
super(TestForTabbedPanel, self).__init__(*args, **kwargs)
Clock.schedule_once(self.on_tab_width, 0.1)
class TabbedPanelApp(App):
def build(self):
return Test2()
if __name__ == '__main__':
TabbedPanelApp().run()
与此相比,另一个线程中提供的不嵌套选项卡面板的解决方案有效;它显示了所有选项卡并且它们已动态调整大小:
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()
如何编写代码,使 TabbedPanel 嵌套在屏幕或其他小部件内,但显示所有选项卡并具有足够的宽度? 我在这里不了解哪些基础知识?
解决方案
详情请参考示例
kv 文件
为 TestForTabbedPanel:
添加 id
GridLayout:
rows:1
TestForTabbedPanel:
id: tp
Python代码
- 将
__init__()
方法从 class TestForTabbedPanel 移动到 class Test2 - 将
super(TestForTabbedPanel, self)
替换为super(Test2, self)
- 将
self.on_tab_width
替换为self.ids.tp.on_tab_width
- 将
pass
添加到 class *TestForTabbedPanel
class Test2(Screen):
def __init__(self, *args, **kwargs):
super(Test2, self).__init__(*args, **kwargs)
Clock.schedule_once(self.ids.tp.on_tab_width, 0.1)
class TestForTabbedPanel(TabbedPanel):
pass
例子
from kivy.app import App
from kivy.uix.tabbedpanel import TabbedPanel, TabbedPanelItem
from kivy.uix.screenmanager import ScreenManager, Screen, SlideTransition
from kivy.uix.boxlayout import BoxLayout
from kivy.clock import Clock
from kivy.lang import Builder
Builder.load_string("""
<Test2>:
GridLayout:
cols:1
GridLayout:
rows:1
Label:
text:'Does it show all 4 tabs?'
GridLayout:
rows:1
TestForTabbedPanel:
id: tp
<CustomWidthTabb@TabbedPanelItem>
width: self.texture_size[0]
padding: 10, 0
size_hint_x: None
<TestForTabbedPanel>:
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 Test2(Screen):
def __init__(self, *args, **kwargs):
super(Test2, self).__init__(*args, **kwargs)
Clock.schedule_once(self.ids.tp.on_tab_width, 0.1)
class TestForTabbedPanel(TabbedPanel):
pass
class TabbedPanelApp(App):
def build(self):
return Test2()
if __name__ == '__main__':
TabbedPanelApp().run()