我正在创建一个简单的绘图应用程序,我想在其中添加一个撤消按钮。到目前为止,我尝试的是:
class DrawScreen(Screen):
r = NumericProperty(0)
g = NumericProperty(0)
b = NumericProperty(0)
brush_width = NumericProperty(2)
def on_touch_down(self, touch):
self.slider = self.ids.slider
if self.slider.collide_point(touch.x, touch.y):
self.brush_width = self.slider.value
else:
self.undo = [touch.x, touch.y]
with self.canvas.before:
Color(self.r, self.g, self.b)
touch.ud["line"] = Line(points=self.undo, width=self.brush_width)
return super(DrawScreen, self).on_touch_down(touch)
def on_touch_move(self, touch):
if self.slider.collide_point(touch.x, touch.y):
self.brush_width = self.slider.value
else:
try:
self.undo += [touch.x, touch.y]
touch.ud["line"].points = self.undo
except:
pass
return super(DrawScreen, self).on_touch_move(touch)
def color(self, r, g, b):
self.r = r
self.g = g
self.b = b
def undo_draw(self):
self.undo = []
此撤消按钮清除列表,但它不会以任何方式影响画布,也不会删除任何行。什么才是解决这个问题的合适方法?在
尝试将它们放入
InstructionGroup
中,然后添加到canvas
中,然后使用canvas.remove(item)
从画布中删除该项。在如果要重做,可能需要保存这些项目。
试试这个例子。我不得不替换
on_touch_down
,当我用鼠标垫移动光标时,它一直在创建指令,在画布上填充孩子:您可能仍然想知道为什么您的代码不起作用。原因是关键字参数
points=self.undo
将复制对self.undo
表示的列表的引用。然后,当代码执行self.undo = []
时,您正在创建一个新的空列表,并告诉self.undo
指向它。但在此之前,self.undo
指向的原始列表仍然存在,points
仍然引用该列表。如果不是
self.undo = []
,而是del self.undo[:]
,那么你可能会完成你想要的。在相关问题 更多 >
编程相关推荐