Kivy:实时更新控件的颜色

2024-04-18 11:54:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我发现了一个类似的问题:

How do I change the color of my widget in Kivy at run time?

我用一种类似的方法来尝试在拖动和移动控件时改变它们的颜色。在


class GraphNode(Widget):
    r = NumericProperty(1.0)

def __init__(self, **kwargs):
    self.size= [50,50]
    self.pos = [175,125]
    super(GraphNode, self).__init__(**kwargs)

def on_touch_down(self, touch):
    if self.collide_point(*touch.pos):
        if touch.grab_current == None:
            self.r = 0.6
            touch.grab(self)             
            return True                
    return super(GraphNode, self).on_touch_down(touch)



def on_touch_move(self, touch):
    if touch.grab_current is self:
        self.pos=[touch.x-25,touch.y-25]
    # now we only handle moves which we have grabbed


def on_touch_up(self, touch):
    if touch.grab_current is self:
        touch.ungrab(self)
        self.r = 1.0
        # and finish up here


def onTouchMove(self, touch):
    if self.collide_point(touch.x, touch.y):
        self.pos=[touch.x-25, touch.y-25]
pass

我尝试使用以下(kv)文件更新数值属性以更改颜色:

^{pr2}$

然而,当我抓住它们的时候,它们的颜色不会改变。如果不更改on_touch_drop()方法中的颜色,则可以使用新颜色生成节点。你知道怎么解决这个问题吗?在


Tags: 方法posselfifinit颜色ondef