如何在kivy中设置微调器内的按钮大小?

时间:2013-11-12 10:21:54

标签: python python-2.7 spinner kivy

请知道如何更改或控制kivy微调器内特殊按钮的大小。注意:我的微调器使用的是kv语言而不是python,看起来像这样:

Spinner:
    id:some_id
    text:"some text"
    values:("1","2","3")
    size_hint:(None,None)
    size: root.width/4,root.height/12

1 个答案:

答案 0 :(得分:2)

Spinner内的按钮是传递给option_cls属性的类型。默认值为SpinnerOption class,它实际上是Button的子类。您可以更改传递给此属性的类(它必须具有text属性和on_release事件)或全局修改SpinnerOption类:

from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.base import runTouchApp

Builder.load_string('''
<SpinnerOption>:
    size_hint: None, None
    size: 20, 20

<MyWidget>:
    Spinner:
        id:some_id
        text:"some text"
        values:("1","2","3")
        size_hint:(None,None)
        size: root.width/4,root.height/12
''')

class MyWidget(BoxLayout):pass

runTouchApp(MyWidget())

使用自定义按钮:

from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.spinner import Spinner
from kivy.base import runTouchApp
from kivy.properties import ObjectProperty

Builder.load_string('''
<MyButton>:
    size_hint: None, None
    size: 20, 20

<MyWidget>:
    MySpinner:
        id:some_id
        text:"some text"
        values:("1","2","3")
        size_hint:(None,None)
        size: root.width/4,root.height/12
''')

class MyButton(Button):
    pass

class MySpinner(Spinner):
    option_cls = ObjectProperty(MyButton) # setting this property inside kv doesn't seem to work

class MyWidget(BoxLayout):
    pass    

runTouchApp(MyWidget())