【发布时间】:2016-04-08 20:08:12
【问题描述】:
我在 Python 2.7 中使用 Kivy。我熟悉如何改变静态按钮本身的颜色,但是当你按下它时如何改变按钮的颜色呢?默认为蓝色。
感谢您的帮助。
【问题讨论】:
我在 Python 2.7 中使用 Kivy。我熟悉如何改变静态按钮本身的颜色,但是当你按下它时如何改变按钮的颜色呢?默认为蓝色。
感谢您的帮助。
【问题讨论】:
根据reference for Button,属性background_down 存储在按下Button 时用于背景的图像的路径。这是默认值:
background_down = StringProperty(
'atlas://data/images/defaulttheme/button_pressed')
您可以将该属性更改为指向不同的image oratlas。
【讨论】:
Kivy 框架为 button_normal 和 button_down 使用背景图像,而 background_color 仅着色,因此在 kv 语言中这可能与您期望的不同:
<Button>:
background_color: 1, 0, 0 # Tints the button red
background_normal: 'images/button_normal.png' # A clear image gives a bright red.
background_down: 'images/button_down.png' # A gray image gives a duller red.
border: (2, 2, 2, 2) # Don't stretch the outer two pixels on each edge when resizing.
这种风格可以让你说一个暗淡的边框和明亮的内部,并在按钮按下时交换它们。如果你使用这个系统,图像将被忽略颜色导入。要解决此问题并解决您的问题,请删除 background_color:
<Button>:
background_normal: 'images/button_normal.png' # Eg. A red button
background_down: 'images/button_down.png' # Eg. A green button
border: (2, 2, 2, 2) # Don't stretch the outer two pixels on each edge when resizing.
这会将按钮的颜色更改为您在图像中所做的任何颜色。值得注意的是,Kivy 非常擅长拉伸图像,所以如果你有单色按钮或小边框,你只需要一个小图像,我使用 8x8 像素。
【讨论】: