如何在运行时更改Kivy中小部件的颜色? [英] How do I change the color of my widget in Kivy at run time?

查看:60
本文介绍了如何在运行时更改Kivy中小部件的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在Kivy中更改简单小部件的颜色. 创建窗口小部件时可以设置颜色,但之后不能更改.

I'm having trouble changing the color of a simple widget in Kivy. I can set the color when I create the widget, but I can't change it afterwards.

这是简单的布局定义文件circletest.kv.它定义了一个圆圈,其中颜色(实际上只是rgba中的r),位置和大小都链接到小部件类中的变量.

Here is the simple layout definition file circletest.kv. It defines a circle where the color (actually just the r, from rgba), position and size are all linked to variables in the widget class.

#:kivy 1.4.1

<CircleWidget>:
    canvas:
        Color:
            rgba: self.r,1,1,1
        Ellipse:
            pos: self.pos
            size: self.size

这是应用程序circletest.py.它创建并显示简单的小部件.创建对象时成功设置了颜色和位置.单击窗口小部件时,窗口小部件可以更改其自身的位置,但是当我尝试更改颜色时,什么也没有发生.

Here's the application circletest.py. It creates and displays the simple widget. The color and position are successfully set when the object is created. When the widget is clicked the widget can change it's own position, but when I try to change the color nothing happens.

import kivy
kivy.require('1.4.1')
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.widget import Widget

Builder.load_file('circletest.kv')

class CircleWidget(Widget):

    def __init__(s, **kwargs):
        s.size= [50,50]
        s.pos = [100,50]
        s.r = 0
        super(CircleWidget, s).__init__(**kwargs)

    def on_touch_down(s, touch):
        if s.collide_point(touch.x,touch.y):    
            s.pos = [s.pos[1],s.pos[0]]           # This works
            s.r = 1.0                       # <---- This does nothing!

class TestApp(App):

    def build(s):
        parent = Widget()
        w = CircleWidget()
        parent.add_widget(w)
        return parent

if __name__ == '__main__':
    TestApp().run()

任何人都可以看到问题吗?

Can anyone see the problem?

更新

仍然不确定该问题的答案是什么,但是我确实有解决方法:

Still not sure what the answer to this question is, but I do have a work around:

在.kv文件中,我将颜色指向对象中的变量.用于提取初始颜色:

In the .kv file I pointed the color to a variable in my object. Works for extracting the initial color:

Color:
    rgba: self.col

当我想从.py文件中更改颜色时,我遍历画布中的所有指令并修改颜色"类型的第一个.显然,这是一种黑客行为,不适用于具有多个Color:属性的窗口小部件:

When I want to change the color from the .py file I loop through all the instructions in the canvas and modify the first one of type "Color". Obviously this is a hack, and won't work on widgets with more than one Color: property:

for i in s.canvas.get_group(None):
    if type(i) is Color:
        i.r, i.g, i.b, i.a = v
        break

我将它们全部包装在一个属性中,因此使用起来很整洁:

I wrapped that all up in a property so it's neat to use:

class CircleWidget(Widget):

    def get_col(s):
        return s._col

    def set_col(s,v):
        for i in s.canvas.get_group(None):
            if type(i) is Color:
                i.r, i.g, i.b, i.a = v
                break
        s._col = v

    col = property(get_col, set_col)

    def __init__(s, **kwargs):
        s.size= [50,50]
        s.pos = [100,50]
        s._col = (1,1,0,1)
        super(CircleWidget, s).__init__(**kwargs)

    def on_touch_down(s, touch):
        if s.collide_point(touch.x,touch.y):    
            s.col = (s.col[::-1]) # Set to some other color

似乎可以立即使用.如果您知道这样做的更好方法,请告诉我.我敢肯定必须有一种更简单的方法,并且我遗漏了一些显而易见的东西!

Seems to work for now. Please let me know if you know a better way of doing this. I'm sure there must be a simpler way, and that I'm missing something obvious!

推荐答案

在您的初始版本中,您只是缺少属性的声明

In your initial version, you were just missing the declaration of the property

from kivy.properties import NumericProperty

在标题和

r = NumericProperty(0)

class CircleWidget(Widget):

另外,您声明您的kv文件名为circletest.kv,但您的应用程序名为TestApp,因此您应更改其中一个以使其连贯,否则将找不到您的kv文件,但您却发现了不会报告任何问题,我想这只是问题中的错别字.刚看到Builder.load_file确定,

also, you state that your kv file is named circletest.kv, but your app is named TestApp, so you should change one of them to make them coherent, or your kv file won't be found, but as you don't report any issue with that, i guess it's only a typo in the question. edit: just saw the Builder.load_file ok,

欢呼.

这篇关于如何在运行时更改Kivy中小部件的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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