2016-03-04 59 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