Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在kivy上再添加一个按钮?_Python_Button_Widget_Kivy - Fatal编程技术网

Python 如何在kivy上再添加一个按钮?

Python 如何在kivy上再添加一个按钮?,python,button,widget,kivy,Python,Button,Widget,Kivy,这是我的密码。 我想再添加一个按钮来停止应用程序。 有两个按钮,一个用于清除画布,另一个用于停止应用程序。 然而,如果我把两者都放在我的程序中,它们中的任何一个都不起作用。如果我把一个注释掉,那么另一个就开始工作了。我想两者都用 from random import random from kivy.app import App from kivy.uix.widget import Widget from kivy.uix.button import Button from kivy.grap

这是我的密码。 我想再添加一个按钮来停止应用程序。 有两个按钮,一个用于清除画布,另一个用于停止应用程序。 然而,如果我把两者都放在我的程序中,它们中的任何一个都不起作用。如果我把一个注释掉,那么另一个就开始工作了。我想两者都用

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()   

在应用程序的
build
方法中,
parent
被覆盖。将定义更改为

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\u应用程序
回调有两行多余的行,应该是

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

谢谢你的回答真的很有帮助!