按钮

_images/button.jpg

: Button 是一个带有相关动作的 Label,当按钮被按下(或在点击/触摸后释放)时触发这些动作。要配置按钮,使用与 Label 类相同的属性(如 padding、font_size 等)和 尺寸系统:

button = Button(text='Hello world', font_size=14)

要在按钮被按下(点击/触摸)时附加回调,请使用 bind:

def callback(instance):
    print('The button <%s> is being pressed' % instance.text)

btn1 = Button(text='Hello world 1')
btn1.bind(on_press=callback)
btn2 = Button(text='Hello world 2')
btn2.bind(on_press=callback)

如果你希望在按钮状态每次变化时都收到通知,可以绑定到 Button.state 属性::

def callback(instance, value):
    print('My button <%s> state is <%s>' % (instance, value))
btn1 = Button(text='Hello world 1')
btn1.bind(state=callback)

Kv 示例:

Button:
    text: 'press me'
    on_press: print("ouch! More gently please")
    on_release: print("ahhh")
    on_state:
        print("my current state is {}".format(self.state))
class kivy.uix.button.Button(**kwargs)[源代码]

基类:ButtonBehavior, Label

Button 类,更多信息请参阅模块文档。

在 1.8.0 版本发生变更: 按钮的行为/逻辑已移至:class:~kivy.uix.behaviors.ButtonBehaviors

background_color

背景颜色,格式为 (r, g, b, a)。

此属性作为纹理颜色的*乘数*。默认纹理为灰色,因此仅设置背景颜色会得到较暗的结果。要设置纯色,请将 background_normal 设置为 ''

在 1.0.8 版本加入.

background_color 是一个 ColorProperty,默认值为 [1, 1, 1, 1]。

在 2.0.0 版本发生变更: ListProperty 更改为 ColorProperty

background_disabled_down

按钮在禁用且按下状态下,用于默认图形表示的背景图像。

在 1.8.0 版本加入.

background_disabled_down 是一个 StringProperty,默认值为 'atlas://data/images/defaulttheme/button_disabled_pressed'。

background_disabled_normal

按钮在禁用且未按下时,用于默认图形表示的背景图像。

在 1.8.0 版本加入.

background_disabled_normal 是一个 StringProperty,默认值为 'atlas://data/images/defaulttheme/button_disabled'。

background_down

按钮被按下时,用于默认图形表示的按钮背景图像。

在 1.0.4 版本加入.

background_down 是一个 StringProperty,默认值为 'atlas://data/images/defaulttheme/button_pressed'。

background_normal

按钮在未按下时用于默认图形表示的背景图像。

在 1.0.4 版本加入.

background_normal 是一个 StringProperty,默认值为 'atlas://data/images/defaulttheme/button'。

border

用于 BorderImage 图形指令的边框。与 background_normalbackground_down 一起使用。可用于自定义背景。

它必须是一个包含四个值的列表:(bottom, right, top, left)。请阅读 BorderImage 指令以了解更多关于如何使用它的信息。

border 是一个 ListProperty,默认值为 (16, 16, 16, 16)。