【问题标题】:How do I easily and cleanly update an image in Kivy widget from python?如何从 python 轻松干净地更新 Kivy 小部件中的图像?
【发布时间】:2019-10-07 06:14:59
【问题描述】:

首先,这是一大段代码,所以为了这个问题,我会尽量简化它。我有一个 Kivy 语言脚本,它有一个根小部件、一个操作栏和 boxlayout。代码的一般结构有点像这样(我不认为这是回答这个问题所必需的,但无论如何都是这样):Root > MenuBarWidg > BoxLayout > Image + Other buttons/labels...。

现在,这是我的小部件在 kivy 中的样子(用于 boxlayout):

<DisplayPhoto>:
    Image:
        id: image_display
        allow_strech: True
        #StringProperty in the class
        source: root.image_path

    Button:

在我的 python 脚本中:

class DisplayPhoto(BoxLayout):
    image_path = StringProperty()
    def __init__(self, **kwargs):
        super(DisplayPhoto, self).__init__(self)
        self.image_path = 'reload.png'

    #this is called from another class on a button press
    def update(self):
        self.image_path = 'new_image_path.png'

在 python 脚本中调用 update 时,没有任何反应。我试过print(self.image_path),它显示new_image_path.png,但它也是一个字符串——不是一个kivy对象。

我已经尝试过诸如通过调用id 等来更新source 等,但没有成功。任何帮助表示赞赏!

【问题讨论】:

标签: python python-3.x image kivy kivy-language


【解决方案1】:

我想问题在于你如何调用 update() 方法。

请参考以下代码

一种方法:

main.py

from kivy.app import App
from kivy.lang import Builder
from kivy.properties import StringProperty
from kivy.uix.boxlayout import BoxLayout

from kivy.config import Config
Config.set('graphics', 'position', 'custom')
Config.set('graphics', 'left', 0)
Config.set('graphics', 'top', 0)

Builder.load_file('main.kv')


class MainView (BoxLayout):
    image_source = StringProperty()

    def __init__(self, **kwargs):
        super(MainView, self).__init__(**kwargs)
        self.image_source = 'pic1.png'


class AnotherClass(BoxLayout):

    def change_image(self):
        app = App.get_running_app()
        app.root.ids['my_image'].source = 'pic2.png'


class ImageChangeApp (App):

    def build(self):
        return MainView()


if __name__ == '__main__':
    ImageChangeApp().run()

main.kv:

<MainView>:
    Image:
        id: my_image
        source: root.image_source
    AnotherClass:


<AnotherClass>:
    Button:
        text: 'Change picture'
        on_release: root.change_image()

另一种方法是使用事件调度器

main.py:

from kivy.app import App
from kivy.lang import Builder
from kivy.properties import StringProperty
from kivy.uix.boxlayout import BoxLayout

from kivy.config import Config
Config.set('graphics', 'position', 'custom')
Config.set('graphics', 'left', 0)
Config.set('graphics', 'top', 0)

Builder.load_file('main.kv')


class MainView (BoxLayout):
    pass


class DisplayPhoto(BoxLayout):
    image_path = StringProperty()

    def __init__(self, **kwargs):
        super(DisplayPhoto, self).__init__(**kwargs)
        self.image_path = 'pic1.png'
        self.register_event_type('on_image_path')

    def on_image_path(self, instance, val):
        print(instance)
        print(val)
        self.image_path = val


class AnotherClass(BoxLayout):

    def change_image(self):
        app = App.get_running_app()
        app.root.ids['display_photo'].dispatch('on_image_path', self, 'pic2.png')


class ImageChangeApp (App):

    def build(self):
        return MainView()


if __name__ == '__main__':
    ImageChangeApp().run()

main.kv

<MainView>:

    DisplayPhoto:
        id: display_photo

    AnotherClass:

<DisplayPhoto>:
    Image:
        source: root.image_path

<AnotherClass>:
    Button:
        text: 'Change picture'
        on_release: root.change_image()

【讨论】:

  • 很抱歉这么晚才看到这个......事件调度程序的第二个例子就像一个魅力。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-24
  • 2018-09-23
  • 2010-11-05
  • 2016-12-06
相关资源
最近更新 更多