Python Kivy:实时更新小部件的颜色
我发现了类似的问题: 我正在使用类似的方法尝试在拖动和移动小部件时更改它们的颜色Python Kivy:实时更新小部件的颜色,python,python-2.7,drawing,kivy,Python,Python 2.7,Drawing,Kivy,我发现了类似的问题: 我正在使用类似的方法尝试在拖动和移动小部件时更改它们的颜色 我正在尝试使用以下(kv)文件更新数值属性以更改颜色: #:kivy 1.0.9 : 节点:图_节点 图形节点: id:图_节点 中心:self.parent.center : 画布: : 尺码:50,50 画布: 椭圆: pos:self.pos 大小:self.size 颜色: rgba:(根.r,1,1,1) : 尺码:10,10 画布: 然而,当我抓住它们时,颜色不会改变。如果我不在on_touch
我正在尝试使用以下(kv)文件更新数值属性以更改颜色:
#:kivy 1.0.9
:
节点:图_节点
图形节点:
id:图_节点
中心:self.parent.center
:
画布:
:
尺码:50,50
画布:
椭圆:
pos:self.pos
大小:self.size
颜色:
rgba:(根.r,1,1,1)
:
尺码:10,10
画布:
然而,当我抓住它们时,颜色不会改变。如果我不在on_touch_drop()方法中更改颜色,那么我可以使用新颜色生成节点。你知道如何解决这个问题吗?看起来你的代码可能工作得很好,但是颜色指令在所有其他指令之后,所以根本不影响任何东西。你是想把它放在椭圆之前吗?我仍然不完全理解这些奇维的东西,但这已经解决了我的问题。谢谢
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
#:kivy 1.0.9
<GraphInterface>:
node: graph_node
GraphNode:
id: graph_node
center: self.parent.center
<GraphApp>:
canvas:
<GraphNode>:
size: 50, 50
canvas:
Ellipse:
pos: self.pos
size: self.size
Color:
rgba: (root.r,1,1,1)
<GraphEdge>:
size: 10, 10
canvas: