如何在Kivy中为小部件/布局设置最小允许宽度/高度?

时间:2014-07-17 12:36:05

标签: python-3.x size kivy python-3.4

我有4个元素的BoxLayout,我需要第一个和最后一个元素来占用最小的可用空间。中间元素具有固定比例(1:1),因此当我调整窗口大小时,侧面元素变得太小而内容会从中移出。我需要例如标签(或按钮,甚至是不同元素的集合)文本始终标签内。这个大小不应该更多,所以我可以说它应该是固定大小,具体取决于它的内容。

更新: 我犯了一个错误,大小可以更多,但不能少。那应该怎么样?

更新: 所以它是我的BoxLayout: enter image description here 当我展开窗口时,只有侧面部分应该展开: enter image description here 当我收缩窗户时,侧面部件应该有一些最小尺寸: enter image description here 所以我认为这是一种固定的最低限度。

1 个答案:

答案 0 :(得分:5)

Label的内容大小可以通过texture_size属性获得,因此您可以将size_hint设置为无,并将大小绑定到内容大小:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button 

from kivy.lang import Builder

kv = '''
<MyButton>:
    size_hint: None, 1
    size: self.texture_size
'''
Builder.load_string(kv)

class MyButton(Button):
    pass

class MyWidget(BoxLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.add_widget(MyButton(text="Fixed size button"))
        self.add_widget(Button(text="Normal button"))
        self.add_widget(MyButton(text="Fixed size button"))         

class MyApp(App):
    def build(self):
        return MyWidget()

if __name__ == '__main__':
    MyApp().run()

您还应该检查text_size属性。来自documentation:&#34;默认情况下,标签不限于任何边界框。您可以使用此属性设置标签的大小约束。文本将自动进入约束。因此,尽管不会减小字体大小,但文本将尽可能地安排在盒子中,并且任何文本仍然在盒子外面剪裁&#34;。例如:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button 

from kivy.lang import Builder

kv = '''
<MyButton>:
    text_size: self.size
    valign: "middle"
    halign: "center"
'''
Builder.load_string(kv)

class MyButton(Button):
    pass

class MyWidget(BoxLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.add_widget(MyButton(text="Example text which is too long to fit in one line"))
        self.add_widget(Button(text="Normal button"))
        self.add_widget(MyButton(text="Example text which is too long to fit in one line"))

class MyApp(App):
    def build(self):
        return MyWidget()

if __name__ == '__main__':
    MyApp().run()

<强>更新 如果您想要更多地控制窗口小部件的缩放方式,可以创建计算窗口小部件值的方法,并将其绑定到更改大小属性(bind或实现on_size())。例如:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.widget import Widget
from kivy.uix.label import Label

from kivy.lang import Builder

from functools import partial

kv = '''
<CentralWidget>:
    pos_hint: {'center_y': .5} 
    size_hint: None, None
    canvas:
        Color:
            rgb: 1, 0, 1
        Rectangle:
            pos: self.pos
            size: self.size

<SideWidget>:
    pos_hint: {'center_y': .5} 
    size_hint: None, 1
    canvas.before:
        Color:
            rgb: 0, 0, 1
        Rectangle:
            pos: self.pos
            size: self.size
'''

Builder.load_string(kv)

class CentralWidget(Widget):
    pass

class SideWidget(Label):
    pass

class MyWidget(BoxLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        w1 = SideWidget(text="............................")
        w2 = CentralWidget()
        w3 = SideWidget(text="............................")

        self.add_widget(w1)
        self.add_widget(w2)
        self.add_widget(w3)

    def on_size(self, *args):
        # self.size - size of parent widget
        # self.children - children of widget
        # self.children[0].texture_size - sife of content of selectend children widget
        # self.children[0].size - size of selected children widget to set
        if((self.size[0]-500)/2 > self.children[0].texture_size[0]):
            self.children[0].size = ((self.size[0]-500)/2, 0)     
            self.children[1].size = (500, 500)   
            self.children[2].size = ((self.size[0]-500)/2, 0)   
        else:
            self.children[1].size = (self.size[0]-2*self.children[0].texture_size[0], self.size[0]-2*self.children[0].texture_size[0])  

class MyApp(App):
    def build(self):
        return MyWidget()

if __name__ == '__main__':
    MyApp().run()