更新画布图片

时间:2017-09-18 11:05:56

标签: python canvas kivy kivy-language

我是kivy的新手,并试图掌握kivy语言背后的概念。我试着让一个按钮在点击时改变它的背景图片。使用我当前的代码,我没有错误,但如果我尝试点击它,按钮不会做任何事情......

这是我的代码:

<ScatterTextWidget>:
orientation: 'vertical'
my_picture: 'picture.png'
Button:
    id: b1
    canvas.after:
        Rectangle:  
            id: m_r
            source: root.my_picture
            pos: self.pos
            size: self.size
    on_release: root.nextPicture()

的.py:

from kivy.app import App

from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout

class ScatterTextWidget(FloatLayout):

    def nextPicture(self):
        self.ids.my_picture = 'newPicture.png'
        self.canvas.ask_update()
        return

class GuiApp(App):
    def build(self):
        return ScatterTextWidget()

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

如何让我的按钮显示新图片?

1 个答案:

答案 0 :(得分:1)

只需更改此行

即可
self.ids.my_picture = 'newPicture.png'

self.my_picture = 'newPicture.png'

您正在访问不是id的属性。