如何动态更改kivy中的图像?

How To Dynamically change Images in kivy?

我正在尝试将以下代码中的背景图像更改为 B_image2.png。我尝试编写一个调用 a 的函数,并在单击图像后更改背景图像的地址,它确实更改了存储在 B_G_IMG 变量中的地址,但屏幕没有更新。

.py 文件

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.image import Image
from kivy.config import Config
from kivy.clock import Clock
from kivy.properties import StringProperty
from kivy.graphics.instructions import InstructionGroup
from kivy.graphics.context_instructions import Color

import random

Config.set('graphics', 'width', '480')
Config.set('graphics', 'height', '320')


running = True


class MyWidget(AnchorLayout):

    LOC = []
    for i in range (10):
        LOC.append((random.randint(0,400),random.randint(0,300)))


    B_G_IMG="B_image.png"


    time_number = StringProperty()

    def __init__(self, **kwargs):
        super(MyWidget, self).__init__(**kwargs)
        self.time_number = str(50)

    def remove_rectangle(self, widget):
        self.grid_layout.remove_widget(widget)
        self.set_level(2)
    def set_level(self,level_num):
        global B_G_ImG
        B_G_IMG = "B_image2.png"
        print B_G_IMG


    def call(self):
        if running:
            #print(self.time_number)
            #self.time_number = str(int(self.time_number)+1)
            pass            
    def clicked(self):
        global running
        #self.time_number = 50
        running=False

    Clock.schedule_interval(call, 1)

    pos1 =(0) #random.randint(-200,200)
    pos2 =(0) #random.randint(-200,200)

class WidgetsApp(App):
    def build(self):
        return MyWidget()


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

.kv 文件

<ImgButton@Button>:

    size_hint:(None,None)
    size:(60,60)

<MyWidget>:
    grid_layout: grid_layout
    AnchorLayout:
        BoxLayout:
            Image:
                source:root.B_G_IMG
        BoxLayout:
            Label:
                text:root.time_number
        FloatLayout:

            id: grid_layout

            ImgButton:
                pos:(root.LOC[0])
                background_normal: 'image.png'
                on_press: root.remove_rectangle(widget=self)
            ImgButton:
                pos:(root.LOC[1])
                background_normal: 'image.png'
                on_press: root.remove_rectangle(widget=self)
            ImgButton:
                pos:(root.LOC[2])
                background_normal: 'image.png'
                on_press: root.remove_rectangle(widget=self)    
            ImgButton:
                pos:(root.LOC[3])
                background_normal: 'image.png'
                on_press: root.remove_rectangle(widget=self)
            ImgButton:
                pos:(root.LOC[4])
                background_normal: 'image.png'
                on_press: root.remove_rectangle(widget=self)
            ImgButton:
                pos:(root.LOC[5])                   
                background_normal: 'image.png'
                on_press: root.remove_rectangle(widget=self)
            ImgButton:
                pos:(root.LOC[6])
                background_normal: 'image.png'
                on_press: root.remove_rectangle(widget=self)
            ImgButton:
                pos:(root.LOC[7])
                background_normal: 'image.png'    
                on_press: root.remove_rectangle(widget=self)
            ImgButton:
                pos:(root.LOC[8])
                background_normal: 'image.png'
                on_press: root.remove_rectangle(widget=self)
            ImgButton:
                pos:(root.LOC[9])
                background_normal: 'image.png'
                on_press: root.remove_rectangle(widget=self)

你只能绑定到 kivy 属性,而不仅仅是任何 python 属性(在这种情况下甚至是 class 属性,这可能不是你想要的)。

您应该将 属性 声明为

B_G_IMG = StringProperty("B_image.png")

编辑:并删除全局内容,您应该通过 class.

的方法使用 self.B_G_IMG 访问它

此外,属性 名称应以小写字母开头,因为 kv 语言使用它在规则中识别它们。在这种情况下它可能会起作用,因为你没有在 kv 中设置 属性,但我建议坚持这个约定(这也是一个 pep8 约定)以避免将来出现问题。