Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在kivy中删除画布上的线条_Python_Kivy_Kivy Language - Fatal编程技术网

Python 如何在kivy中删除画布上的线条

Python 如何在kivy中删除画布上的线条,python,kivy,kivy-language,Python,Kivy,Kivy Language,我正在创建一个简单的绘图应用程序,我想在其中添加一个撤消按钮。到目前为止,我尝试的是: class DrawScreen(Screen): r = NumericProperty(0) g = NumericProperty(0) b = NumericProperty(0) brush_width = NumericProperty(2) def on_touch_down(self, touch): self.slider = sel

我正在创建一个简单的绘图应用程序,我想在其中添加一个撤消按钮。到目前为止,我尝试的是:

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 = []

此“撤消”按钮将清除列表,但不会以任何方式影响画布,也不会删除任何行。什么是合适的方法呢?

尝试将它们放入
说明组
,然后添加到
画布
,然后使用
画布从画布中删除项目。删除(项目)

如果要重做,可能需要保存项目。
试试这个例子。我不得不在触摸屏上替换
,当我用鼠标垫移动光标时,它不断地创建指令,在画布上填充儿童:

from kivy.app import App
from kivy.lang import Builder
from kivy.graphics import Line, Color, InstructionGroup
from kivy.uix.widget import Widget


class MyWidget(Widget):

    undolist = []
    objects = []
    drawing = False

    def on_touch_up(self, touch):
        self.drawing = False

    def on_touch_move(self, touch):
        if self.drawing:
            self.points.append(touch.pos)
            self.obj.children[-1].points = self.points
        else:
            self.drawing = True
            self.points = [touch.pos]
            self.obj = InstructionGroup()
            self.obj.add(Color(1,0,0))
            self.obj.add(Line())
            self.objects.append(self.obj)
            self.canvas.add(self.obj)


    def undo(self):
        item = self.objects.pop(-1)
        self.undolist.append(item)
        self.canvas.remove(item)

    def redo(self):
        item = self.undolist.pop(-1)
        self.objects.append(item)
        self.canvas.add(item)


KV = """

BoxLayout:
    MyWidget:
        id: widget
    Button:
        text: "undo"
        on_release:
            widget.undo()
    Button:
        text: "redo"
        on_release:
            widget.redo()


"""


class MyApp(App):

    def build(self):
        root = Builder.load_string(KV)
        return root

MyApp().run()

您可能仍然想知道为什么代码不起作用。原因是关键字参数
points=self.undo
将复制对
self.undo
表示的列表的引用。然后,当代码执行
self.undo=[]
时,您正在创建一个新的空列表,并告诉
self.undo
指向它。但是在此之前指向的原始列表仍然存在,
self.undo
仍然引用该列表。

如果您没有执行
self.undo=[]
,而是执行了
del self.undo[:]
,您可能会实现您想要的。

@Baxorr您很好。