2015-11-19 80 views
1

這個例子的頂部 - 「Kivy藍圖」 它的工作原理,但是,在繪製,線條,油漆過的按鈕。請幫助解決此問題Kivy畫布組件借鑑了書的標誌Vasilkov按鈕

... 
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文件上的按鈕'清除'和'ColorButtons'。

<CanvasWidget>: 
Button: 
on_release: root.clear_canvas() 
text: 'Clear' 
... 
<[email protected]>: 
on_release: app.canvas_widget.set_color(self.background_color) 
... 
+0

究竟什麼是你問?什麼是錯的,你希望它有什麼不同? – inclement

回答

1

使用canvas.before代替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) 
... 
+0

那正是我想要的,非常感謝你的配合! –

+0

這也適用於kv文件!使用'canvas.before:'而不是'canvas:'! –