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

时间:2015-04-18 06:51:14

标签: python-2.7 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)

1 个答案:

答案 0 :(得分:0)

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

相反,您应该将该属性声明为

B_G_IMG = StringProperty("B_image.png")

编辑:删除全局内容,您应该使用self.B_G_IMG从类的方法中访问它。

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