2016-03-04 33 views
0

我有一個繪畫應用程序的這個kivy代碼。在按鈕上繪圖問題

<PaintApp>: 
    name: "PaintApp" 
    BoxLayout: 
     orientation:'vertical' 
     CanvasWidget: 
      size_hint_y:.9 
     BoxLayout: 
      size_hint:(1,None) 
      size_hint_y:.1 
      orientation: 'horizontal' 
      Button: 
       text: 'Delete' 
       font_size: 20 
       # on_release: root.clear_canvas() 
       background_normal: 'red_button_normal.png' 
       background_down: 'red_button_down.png' 
       border: (2, 2, 2, 2) 
       right: root.right 
       top: root.top 
       width: 80 
       height: 40 
      Button: 
       text: 'Back' 
       font_size: 20 
       # on_release: app.root.current= 'ActivitySelectScreen' 
       background_normal: 'red_button_normal.png' 
       background_down: 'red_button_down.png' 
       border: (2, 2, 2, 2) 
       right: root.right -80 
       top: root.top 
       width: 80 
       height: 40 

      LineWidthButton: 
       text: 'Thin' 

      LineWidthButton: 
       text: 'Normal' 
       state: 'down' 

      LineWidthButton: 
       text: 'Thick' 


      ColorButton: 
       background_color: C('#2980b9') 
       state: 'down' 

      ColorButton: 
       background_color: C('#16a085') 

      ColorButton: 
       background_color: C('#27ae60') 

      ColorButton: 
       background_color: C('#f39c12') 

      ColorButton: 
       background_color: C('#d35400') 

      ColorButton: 
       background_color: C('#c0392b') 

      ColorButton: 
       background_color: C('#8e44ad') 

      ColorButton: 
       background_color: C('#bdc3c7') 

      ColorButton: 
       background_color: C('#7f8c8d') 

      ColorButton: 
       background_color: C('#2c3e50') 

Python代碼:

class CanvasWidget(Widget): 
    line_width = 2 
    def on_touch_down(self, touch): 
     if Widget.on_touch_down(self, touch): 
      return 

     with self.canvas: 
      touch.ud['current_line'] = Line(
       points=(touch.x, touch.y), 
       width=self.line_width) 

    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.last_color = new_color 
     self.canvas.add(Color(*new_color)) 

    def set_line_width(self, line_width='Normal'): 
     self.line_width = {'Thin': 1, 'Normal': 2, 'Thick': 4}[line_width] 

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

輸出的應用程序是這樣的:

它在某種程度上借鑑的按鈕也落後。我不明白爲什麼。當我在框佈局中添加兩個按鈕時。它不會在其他頂部添加一個。

回答

2

第一件事:該代碼是不完整的,LineWidthButtonColorButton在Python代碼沒有定義,甚至沒有在KV文件,他們只使用。有各種東西缺少進口。當你問的時候不要這麼做......而且,這種行爲不是問題。

除此之外,我無法運行它,從您的描述中可以看出,您的CanvasWidget確實使用整個畫布,而不僅僅是小部件的大小/空間,即圖形將顯示在整個屏幕上。這個「修復」是StencilView。您可以在this demo中清楚地看到圖形留在邊界框內。

和按鈕,那麼這是一個BoxLayout,並有權在docs頁面的頂部爲BoxLayout,你有一個展示的佈局如何表現一個GIF:

enter image description here

如果要放置Button另一按鈕,還有其他佈局的行爲。例如:FloatLayout

enter image description here