在Kivy中更改按钮的背景颜色 [英] Changing the background color of a Button in Kivy

查看:488
本文介绍了在Kivy中更改按钮的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Kivy的新手,无法指定Button的背景颜色.这是我的简单示例:

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中的Button指定背景颜色的方法吗?

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_normalbackground_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中更改按钮的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆