問題描述
我是 Kivy 的新手,無法指定按鈕的背景顏色.這是我的簡單示例:
I'm new to Kivy and having trouble specifying the background color of a Button. Here's my simple example:
# 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()
以及隨附的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:
這不是為 Kivy 中的按鈕指定背景顏色的方法嗎?
Is this not the way to specify the background color for a Button in Kivy?
謝謝!
推薦答案
啊,這是一個常見的混淆.問題是 Button.background_color
確實是一種 tint,而不僅僅是塊顏色.由于默認背景是灰色圖像(您通常會在制作無樣式按鈕時看到該圖像),因此您最終看到的是該灰色圖像的紅色調 - 您觀察到的暗紅色.
Ah, this is a common confusion. The problem is that Button.background_color
really 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.
您可以通過將背景圖像替換為純白色(不必超過幾個像素)或使用 background_normal
來獲得所需的行為和 background_down
屬性.當您的 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_normal
and background_down
properties. When your background_color tints the new pure white image, you get the pure red you're after.
我想這在文檔中不是很清楚,我會嘗試改進它.
I guess this isn't so clear in the docs, I'll try to improve it.
這篇關于在 Kivy 中更改按鈕的背景顏色的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!