我是kivy的新手,在为我的小部件创建触摸事件时遇到了一些麻烦。
我的想法是,我将在散布布局中有许多(生成的)方形单元格,并且每个单元格都有自己的on_touch_down
个事件。但是,当我右键单击其中一个单元格时,偶数始终会触发创建的最后一个单元格 - 在本例中为x=99 y=99
。我必须误解有关均匀传播的工作原理,我会感激任何帮助。
细胞类:
class cell(Widget):
r = NumericProperty(1)
g = NumericProperty(1)
b = NumericProperty(1)
color = ReferenceListProperty(r,g,b)
def __init__(self, id_x, id_y, **kwargs):
super(cell, self).__init__(**kwargs)
self.id_x=id_x
self.id_y=id_y
def on_touch_down(self, touch):
if touch.button=="right":
self.test()
return True
else:
super(cell, self).on_touch_down
def test(self):
print"*****"
print "x = " + str(self.id_x)
print "y = " + str(self.id_y)
print "size = " + str(self.size)
print "pos = " + str(self.pos)
self.color = (0,0,1)
在app类中构建单元格:
def build(self):
b = base() #this is just an empty BoxLayout
startx = 0
starty = 0
for x in range(100):
tmpArr = []
for y in range(100):
cellRef = cell(id_x = x, id_y=y,size=(10,10),pos=(startx, starty))
b.mainLayout.add_widget(cellRef)
tmpArr.append(cellRef)
startx += 11
allCells.append(tmpArr)
starty += 11
startx = 0
return b
kv文件:
<base>:
orientation:"vertical"
mainLayout: layoutS
ScatterPlaneLayout:
id:layoutS
do_collide_after_children:True
auto_bring_to_front: False
scale_min:.3
scale_max:5.
do_rotation: False
<cell>:
size_hint:(None,None)
canvas:
Color:
rgb: self.color
Rectangle:
size:self.size
pos:self.pos