KIVY Button¶

Button --- Kivy 2.3.0 documentation

Button

Jump to API


Module: kivy.uix.button

Added in 1.0.0

The Button is a Label with associated actions that are triggered when the button is pressed (or released after a click/touch). To configure the button, the same properties (padding, font_size, etc) and sizing system are used as for the Label class:

按钮(Button)是一个带有相关动作的标签(Label),这些动作在按钮被按下(或点击/触摸后释放)时触发。为了配置按钮,我们使用了与标签(Label)类相同的属性(如内边距padding、字体大小font_size等)和尺寸系统。

python 复制代码
button = Button(text='Hello world', font_size=14)

To attach a callback when the button is pressed (clicked/touched), use bind:

当按钮被按下(点击/触摸)时,要附加一个回调函数,可以使用bind方法。

python 复制代码
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)

If you want to be notified every time the button state changes, you can bind to the Button.state property:

如果你想要在每次按钮状态发生变化时都收到通知,你可以将事件绑定到Button.state属性上:

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

Kv Example:

python 复制代码
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))

APIHide Description ⇑


class kivy.uix.button.Button(**kwargs )

Bases: kivy.uix.behaviors.button.ButtonBehavior, kivy.uix.label.Label

Button class, see module documentation for more information.

Changed in version 1.8.0: The behavior / logic of the button has been moved to ButtonBehaviors.

background_color

Background color, in the format (r, g, b, a). 背景颜色,格式为(r, g, b, a)。

This acts as a multiplier to the texture color. The default texture is grey, so just setting the background color will give a darker result. To set a plain color, set the background_normal to ''.

它作为纹理颜色的一个乘数因子。默认纹理是灰色的,所以仅仅设置背景颜色会得到一个更暗的结果。要设置纯色,请将background_normal属性设置为空字符串('')。

New in version 1.0.8. 此功能在版本1.0.8中新增。

The background_color is a ColorProperty and defaults to [1, 1, 1, 1].
background_color是一个ColorProperty,其默认值为[1, 1, 1, 1](即白色,完全不透明)。

Changed in version 2.0.0: Changed from ListProperty to ColorProperty.

在版本2.0.0中更改:从ListProperty更改为ColorProperty。

background_disabled_down

Background image of the button used for the default graphical representation when the button is disabled and pressed.

New in version 1.8.0.

background_disabled_down is a StringProperty and defaults to 'atlas://data/images/defaulttheme/button_disabled_pressed'.

background_disabled_normal

Background image of the button used for the default graphical representation when the button is disabled and not pressed.

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

New in version 1.8.0. 此功能在版本1.8.0中新增。

background_disabled_normal is a StringProperty and defaults to 'atlas://data/images/defaulttheme/button_disabled'.

background_down

Background image of the button used for the default graphical representation when the button is pressed.

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

New in version 1.0.4. 此功能在版本1.0.4中新增。

background_down is a StringProperty and defaults to 'atlas://data/images/defaulttheme/button_pressed'.
background_disabled_down是一个StringProperty,其默认值为'atlas://data/images/defaulttheme/button_disabled_pressed'

background_normal

Background image of the button used for the default graphical representation when the button is not pressed.

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

New in version 1.0.4. 此功能在版本1.0.4中新增。

background_normal is a StringProperty and defaults to 'atlas://data/images/defaulttheme/button'.
background_normal是一个StringProperty,其默认值为 'atlas://data/images/defaulttheme/button'

border

Border used for BorderImage graphics instruction. Used with background_normal and background_down. Can be used for custom backgrounds.

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

It must be a list of four values: (bottom, right, top, left). Read the BorderImage instruction for more information about how to use it.

它必须是一个包含四个值的列表:(底部, 右侧, 顶部, 左侧)。要获取更多关于如何使用它的信息,请阅读BorderImage指令的说明。

border is a ListProperty and defaults to (16, 16, 16, 16)
border是一个ListProperty,其默认值为(16, 16, 16, 16)。

相关推荐
Narutolxy10 分钟前
Python 单元测试:深入理解与实战应用20240919
python·单元测试·log4j
Amo Xiang33 分钟前
2024 Python3.10 系统入门+进阶(十五):文件及目录操作
开发语言·python
liangbm343 分钟前
数学建模笔记——动态规划
笔记·python·算法·数学建模·动态规划·背包问题·优化问题
B站计算机毕业设计超人1 小时前
计算机毕业设计Python+Flask微博情感分析 微博舆情预测 微博爬虫 微博大数据 舆情分析系统 大数据毕业设计 NLP文本分类 机器学习 深度学习 AI
爬虫·python·深度学习·算法·机器学习·自然语言处理·数据可视化
羊小猪~~1 小时前
深度学习基础案例5--VGG16人脸识别(体验学习的痛苦与乐趣)
人工智能·python·深度学习·学习·算法·机器学习·cnn
waterHBO3 小时前
python 爬虫 selenium 笔记
爬虫·python·selenium
编程零零七4 小时前
Python数据分析工具(三):pymssql的用法
开发语言·前端·数据库·python·oracle·数据分析·pymssql
AIAdvocate6 小时前
Pandas_数据结构详解
数据结构·python·pandas
小言从不摸鱼6 小时前
【AI大模型】ChatGPT模型原理介绍(下)
人工智能·python·深度学习·机器学习·自然语言处理·chatgpt
FreakStudio8 小时前
全网最适合入门的面向对象编程教程:50 Python函数方法与接口-接口和抽象基类
python·嵌入式·面向对象·电子diy