迫不及待地重新加载图像

Kivy reload image

我正在创建一个简单的应用程序,它抓取一个字符串并将其转换为二维码,但我无法让我的应用程序更新到 return 更新的二维码图像。文件已更新,但即使我调用 .reload(),结果显示的图像仍然是以前创建的图像。

我相信在启动应用程序期间,正在使用当前存储的图像构建应用程序,一个来自以前的 运行,为什么 .reload() 不更新它?我使用正确吗?

旁注:有没有办法增加调试的详细程度,这样我不仅可以看到 kivy 的 python 错误? 运行 一切都通过尝试:有点压倒性。

class Note(Screen):
def reload_image(self):
        w = NoteCode().ids.image
        w.reload()
def qr_generator(self):
    note_input = self.note_input.text
    inn = pyqrcode.create(note_input)
    with open('qr.png','w') as inputfile:
        inn.png(inputfile,scale=8)
    self.reload_image()

对应的kv:

<Note>:
    note_input:note_input
    name:"Note"
GridLayout:
    rows:3
    cols:1
    Label:
        size_hint_y:0.15
        text:"Create a QR Note"
    TextInput:
        id:note_input
    GridLayout:
        size_hint_y:0.15
        rows:1
        cols:2
        Button:
            text:"Back"
            on_release: app.root.current = "Main"
        Button:
            text:"Generate"
            on_release: root.qr_generator()
            on_release: app.root.current = "NoteCode"


<NoteCode>:
    name:"NoteCode"
    GridLayout:
    cols:1
    rows:2
    Image:
        id:image
        allow_stretch:False
        source:'qr.png'
    Button:
        size_hint_y:0.1
        text:"Back"
        on_release: app.root.current = "Note"

对我来说,这一行 w = NoteCode().ids.image 似乎有问题,因为你在那里创建了一个新实例并且没有得到现有的实例,因此即使你重新加载一些东西,它也不会是 NoteCode 最有可能在布局中使用。您需要通过某些 id 访问 NoteCode,或者将其放入其 __init__:

self.app = App.get_running_app()
self.app.notecode = self

然后在 Note.reload() 中执行此操作:

app = App.get_running_app()
w = app.notecode.ids.image.reload()

您也可以将应用程序放入 init 或基本上任何您喜欢的地方,重要的是获得正确的实例。