2016-09-16 141 views
0

我第一次使用Kivy庫,並且我正在嘗試開發一個簡單的UI,使用TabbedPanel。我會設置每個標籤(代碼TabbedPanelItem)的大小(x)以適合整個TabbedPanel的寬度,但如果我在.kv文件中使用heightsize_hint,似乎它們不起作用。Python(Kivy) - 如何調整TabbedPanel的標籤頁的大小

這裏我千伏代碼:

#: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() 

我讀過有關使用StripLayoutTabbedPanel內,但我不知道是否是一個很好的解決方案以及如何正確應用它。有什麼建議麼?

回答

2

我已經試驗了一下你的代碼,在閱讀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更少或更多的標籤,一個水平滾動條將通過添加額外的標籤滾動。

我希望我是有幫助的。

+0

謝謝!我完全錯過了文檔XD的一小部分! – Drakenden

相關問題