按钮未调整大小以适合文本(Kivy)

时间:2014-10-11 19:33:34

标签: python kivy

Sc.kv

<Selector>:
    pos: 200, 400

<ScLayout>:
    cols: 2
    pos: self.parent.pos
    spacing: 10

<Button>:
    width: len(self.text) * 50

FloatLayout:
    Selector:
        ScLayout:
            Button:
                text: 'hey'
            Button:
                text: 'welcome'
            Button:
                text: 'hi'
            Button:
                text: 'how are you'

和ScApp.py

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button

class Selector(Widget): pass

class ScLayout(GridLayout): pass

class ScApp(App):

    def build(self):
        pass

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

我希望按钮调整大小以适应文本的大小,但是会发生什么: (not enough rep to embed images!)

为什么会这样?,我该如何解决?

2 个答案:

答案 0 :(得分:1)

我写了blog post来涵盖这个问题。关键是绑定Button大小以跟踪其texture_size。

答案 1 :(得分:1)

<Selector>:
    pos: 200, 400

<ScLayout>:
    cols: 2
    pos: self.parent.pos
    spacing: 10

<Button>:
    width: len(self.text) * 50

FloatLayout:
    Selector:
        ScLayout:
            Button:
                text: 'hey'
                text_size: self.width, None
                halign:'center'
                

        Button:
            text: 'welcome'
            text_size: self.width, None
            halign:'center'

        Button:
            text: 'hi'
            text_size: self.width, None
            halign:'center'
        Button:
            text: 'how are you'
            text_size: self.width, None
            halign:'center'