2016-04-04 20 views
0

これは私のコードです。 アプリケーションを停止するもう1つのボタンを追加したいと思います。 2つのボタンがあります.1つはキャンバスをクリアするボタン、もう1つはアプリを停止するボタンです。 しかし、両方とも自分のプログラムに入れても、いずれかが動作しません。私がコメントすると、別のものが動作し始めます。私は両方を使いたいです。 parentが上書きされます、アプリケーションのbuild方法では、kivyに1つ以上のボタンを追加するにはどうすればいいですか?

from random import random 
from kivy.app import App 
from kivy.uix.widget import Widget 
from kivy.uix.button import Button 
from kivy.graphics import Color, Ellipse, Line 


class MyPaintWidget(Widget): 

    def on_touch_down(self, touch): 
     color = (random(), 1, 1) 
     with self.canvas: 
      Color(*color, mode='hsv') 
      d = 1. 
      Ellipse(pos=(touch.x - d/2, touch.y - d/2), size=(d, d)) 
      touch.ud['line'] = Line(points=(touch.x, touch.y)) 

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


class MyPaintApp(App): 

    def build(self): 
     parent = Widget() 
     self.painter = MyPaintWidget() 
     clearbtn = Button(text='Clear') 
     clearbtn.bind(on_release=self.clear_canvas) 
     parent.add_widget(self.painter) 
     parent.add_widget(clearbtn) 

     parent = Widget() 
     self.painter = MyPaintWidget() 
     quitbtn = Button(pos=(100,0),text='quit') 
     quitbtn.bind(on_release=self.quit_app) 
     parent.add_widget(self.painter) 
     parent.add_widget(quitbtn) 
     return parent   

    def clear_canvas(self, obj): 
     self.painter.canvas.clear() 

    def quit_app(self,obj): 
     btn1=Button(pos=(width,0),text="QUIT") 
     btn1.bind(on_release=self.quit_app) 
     App.get_running_app().stop() 



if __name__=="__main__": 
    MyPaintApp().run() 

答えて

0

。両方のボタンを表示するには、定義を

def build(self): 
    parent = Widget() 
    self.painter = MyPaintWidget() 
    clearbtn = Button(text='Clear') 
    clearbtn.bind(on_release=self.clear_canvas) 
    parent.add_widget(self.painter) 
    parent.add_widget(clearbtn) 

    quitbtn = Button(pos=(100,0),text='quit') 
    quitbtn.bind(on_release=self.quit_app) 
    parent.add_widget(quitbtn) 
    return parent 

に変更します。また、quit_appコールバックには余分な2行があります。

def quit_app(self,obj): 
    App.get_running_app().stop() 
+0

ありがとうございます!あなたの答えは本当に役に立ちました! –

関連する問題