Python 在 Kivy 中更改按钮的背景颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20181250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing the background color of a Button in Kivy
提问by Fiver
I'm new to Kivy and having trouble specifying the background color of a Button. Here's my simple example:
我是 Kivy 的新手,无法指定按钮的背景颜色。这是我的简单示例:
# custombutton.py
from kivy.app import App
from kivy.uix.widget import Widget
class MyWidget(Widget):
pass
class CustomButtonApp(App):
def build(self):
return MyWidget()
if __name__ == '__main__':
CustomButtonApp().run()
And the accompanying kv file custombutton.kv:
以及随附的 kv 文件custombutton.kv:
#:kivy 1.7.2
<MyWidget>:
canvas:
Color:
rgb: (0.93, 0.93, 0.93)
Rectangle:
pos: self.pos
size: self.size
Button:
center: self.parent.center
font_size: 14
height: 28
background_color: (1.0, 0.0, 0.0, 1.0)
text: "I'm a Button"
I'm sure I'm missing something obvious, but I've been messing with this for over an hour now and getting nowhere. The button seems to get colored a hint of very dark red:
我确定我遗漏了一些明显的东西,但我已经搞砸了一个多小时,但一无所获。该按钮似乎染上了一丝非常深的红色:


Is this not the way to specify the background color for a Button in Kivy?
这不是在 Kivy 中为 Button 指定背景颜色的方法吗?
Thanks!
谢谢!
采纳答案by inclement
Ah, this is a common confusion. The problem is that Button.background_colorreally works as a kind of tint, not just a block colour. Since the default background is a grey image (the one you normally see if you make an unstyled button), what you end up seeing is a red tint to that grey image - which comes out as the dark red you observe.
啊,这是一个常见的困惑。问题是它Button.background_color真的可以作为一种色调,而不仅仅是块颜色。由于默认背景是灰色图像(如果您制作无样式按钮,您通常会看到该图像),因此您最终看到的是该灰色图像的红色调 - 显示为您观察到的深红色。
You can get the behaviour you want by replacing the background image to just one that's plain white (it doesn't have to be more than a few pixels), or by otherwise playing with the background_normaland background_downproperties. When your background_color tints the new pure white image, you get the pure red you're after.
您可以通过将背景图像替换为纯白色(不必超过几个像素)或以其他方式使用background_normal和background_down属性来获得所需的行为。当您的 background_color 为新的纯白色图像着色时,您会得到您想要的纯红色。
I guess this isn't so clear in the docs, I'll try to improve it.
我想这在文档中不是很清楚,我会尝试改进它。
回答by Taylor D
It's been a while since this was first posted so maybe with updates they came up with a better solution:
自从第一次发布以来已经有一段时间了,所以也许随着更新他们想出了更好的解决方案:
Button:
background_normal: ''
background_color: 1, .3, .4, .85
Since the Button has a default grey, adding background color will only tint the button. By setting background_normal to '' that resets the default to white. From the white canvas the background_color works as you would expect.
由于 Button 有一个默认的灰色,添加背景颜色只会给按钮着色。通过将 background_normal 设置为 '' 将默认值重置为白色。从白色画布中,background_color 可以按您的预期工作。
Documentation
文档
1) https://kivy.org/docs/api-kivy.uix.button.html?highlight=button#module-kivy.uix.button
1) https://kivy.org/docs/api-kivy.uix.button.html?highlight=button#module-kivy.uix.button

