如何從Python中輕鬆清潔地更新Kivy小工具中的圖像?

2024-04-25 06:03:32 发布

您现在位置:Python中文网/ 问答频道 /正文

首先,这是一段很大的代码,所以为了这个问题,我会尽量简化它。我有一个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等等,但是没有成功。感谢您的帮助!你知道吗


Tags: path代码imageself脚本idpng部件
1条回答
网友
1楼 · 发布于 2024-04-25 06:03:32

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

请参考以下代码

一种方法是:

你知道吗主.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()

你知道吗主电源.kv地址:

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


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

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

你知道吗主.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()

你知道吗主电源.kv你知道吗

<MainView>:

    DisplayPhoto:
        id: display_photo

    AnotherClass:

<DisplayPhoto>:
    Image:
        source: root.image_path

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

相关问题 更多 >