Python Kivy Canvas小部件在按钮顶部绘制

Python Kivy Canvas小部件在按钮顶部绘制,python,kivy,Python,Kivy,这个例子来自马克·瓦西尔科夫的书——《基维蓝图》 它可以工作,但是,在绘制时,在按钮上绘制线条。请帮忙解决这个问题 ... class CanvasWidget(Widget): def on_touch_down(self, touch): if Widget.on_touch_down(self, touch): return True print(touch.x, touch.y) with self.canvas: Co

这个例子来自马克·瓦西尔科夫的书——《基维蓝图》 它可以工作,但是,在绘制时,在按钮上绘制线条。请帮忙解决这个问题

...   
class CanvasWidget(Widget):  

def on_touch_down(self, touch):
    if Widget.on_touch_down(self, touch):
        return True
    print(touch.x, touch.y)
    with self.canvas:
        Color(*get_color_from_hex('#0080FF80'))
        Line(circle=(touch.x, touch.y, 2), width=2)
        touch.ud['current_line'] = Line(points=(touch.x, touch.y), width=2)     

def on_touch_move(self, touch):                               
    if 'current_line' in touch.ud:
        touch.ud['current_line'].points += (touch.x, touch.y)

def set_color(self, new_color):
    self.canvas.add(Color(*new_color))    

def clear_canvas(self):        
    self.canvas.clear()
    saved = self.children[:]
    self.clear_widgets()        
    for widget in saved:
        self.add_widget(widget)

class PaintApp(App):

def build(self):
    self.canvas_widget = CanvasWidget()
    self.canvas_widget.set_color(get_color_from_hex('#2980B9'))
    return self.canvas_widget

if __name__ == '__main__':
PaintApp().run()
Kv文件上的“清除”按钮和“彩色按钮”

<CanvasWidget>:
Button:  
on_release: root.clear_canvas()
text: 'Clear'
...
<ColorButton@RadioButton>:
on_release: app.canvas_widget.set_color(self.background_color)
...
:
按钮:
发布时:root.clear\u canvas()
文本:“清除”
...
:
发布时:app.canvas\u widget.set\u color(self.background\u color)
...

使用
canvas。在
之前,而不是
canvas

...
def on_touch_down(self, touch):
    if Widget.on_touch_down(self, touch):
        return True
    print(touch.x, touch.y)
    with self.canvas.before:
        Color(*get_color_from_hex('#0080FF80'))
        Line(circle=(touch.x, touch.y, 2), width=2)
        touch.ud['current_line'] = Line(points=(touch.x, touch.y), width=2)
...

你到底在问什么?有什么问题,你希望它有什么不同?这也适用于kv文件!使用
canvas.before:
而不是
canvas: