【问题标题】:Python (Kivy) - How to resize tabs of a TabbedPanelPython (Kivy) - 如何调整 TabbedPanel 的标签大小
【发布时间】:2017-01-24 09:19:13
【问题描述】:

我是第一次使用Kivy 库,我正在尝试使用TabbedPanel 开发一个简单的用户界面。我会设置每个选项卡的大小(x)(在代码TabbedPanelItem 中)以适应整个TabbedPanel 的宽度,但如果我在.kv 文件中使用height 或size_hint,似乎它们不起作用。

这是我的 kv 代码:

#:import sm kivy.uix.screenmanager
ScreenManagement:
    transition: sm.FadeTransition()
    SecondScreen:


<SecondScreen>:
    tabba: tabba
    name: 'second'
    FloatLayout:
        background_color: (255, 255, 255, 1.0)
        BoxLayout:
            orientation: 'vertical'
            size_hint: 1, 0.10
            pos_hint: {'top': 1.0}
            canvas:
                Color:
                    rgba: (0.98, 0.4, 0, 1.0)
                Rectangle:
                    pos: self.pos
                    size: self.size
            Label:
                text: 'MyApp'
                font_size: 30
                size: self.texture_size

        BoxLayout:
            orientation: 'vertical'
            size_hint: 1, 0.90
            Tabba:
                id: tabba

        BoxLayout:
            orientation: 'vertical'
            size_hint: 1, 0.10
            pos_hint: {'bottom': 1.0}
            Button:
                background_color: (80, 1, 0, 1.0)
                text: 'Do nop'
                font_size: 25


<Tabba>:
    do_default_tab: False
    background_color: (255, 255, 255, 1.0)
    # I would these three tabs' width filling the entire TabbedPanel's width
    TabbedPanelItem:
        text: 'First_Tab'
        Tabs:

    TabbedPanelItem:
        text: 'Second_Tab'
        Tabs:

    TabbedPanelItem:
        text: 'Third_Tab'
        Tabs:


<Tabs>:
    grid: grid
    ScrollView:
        do_scroll_y: True
        do_scroll_x: False
        size_hint: (1, None)
        height: root.height
        GridLayout:
            id: grid
            cols: 1
            spacing: 10
            padding: 10
            size_hint_y: None
            height: 2500

这是我的 Python 代码:

# coding=utf-8
__author__ = 'drakenden'

__version__ = '0.1'

import kivy
kivy.require('1.9.0') # replace with your current kivy version !

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.properties import StringProperty, ObjectProperty,NumericProperty
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.utils import platform
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.scrollview import ScrollView

class Tabs(ScrollView):
    def __init__(self, **kwargs):
        super(Tabs, self).__init__(**kwargs)


class Tabba(TabbedPanel):
    pass


class SecondScreen(Screen):
    pass

class ScreenManagement(ScreenManager):
    pass

presentation = Builder.load_file("layout2.kv")

class MyApp(App):

    def build(self):
        return presentation


MyApp().run()

我读过关于在TabbedPanel 中使用StripLayout,但我不知道这是否是一个好的解决方案以及如何正确应用它。有什么建议?

【问题讨论】:

    标签: python user-interface layout kivy kivy-language


    【解决方案1】:

    我对您的代码进行了一些实验,在阅读了 TabbedPanel 文档后,我发现tab_width 指定了选项卡标题的宽度(因为 tab_height 是高度)。要在您的 kivy 文件中使用它,您必须添加以下行:

    <Tabba>:
        do_default_tab: False
        tab_width: self.parent.width / 3
        background_color: (255, 0, 255, 1.0)
        # I would these three tabs' width filling the entire TabbedPanel's width
        TabbedPanelItem:
        .
        .
        .
        the rest of your kivy file
    

    我们在这一行中添加的是每个选项卡将是其父宽度的 1/3。

    这甚至适用于少于或多于 3 个的选项卡。如果您添加更多选项卡,将添加一个水平滚动条以滚动浏览额外的选项卡。

    希望对你有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-21
      • 1970-01-01
      • 1970-01-01
      • 2014-02-16
      • 2019-01-05
      • 1970-01-01
      • 2012-07-23
      相关资源
      最近更新 更多