从文件对话框加载图像有什么建议吗?

Any suggestions for loading an Image from a file dialog?

我正在为我的应用程序构建 GUI,我正在尝试从文件对话框加载图像。 有人有什么建议吗?这是我的第一个 Kivy 应用程序,有时我无法完全理解文档。我尝试了多种方法,我得到的最好结果是屏幕左下方的图像。

我试过 Placing an image in the middle of the label in Kivy 的解决方案,但没有解决我的问题。

我的 .kv 文件的一部分:

<RunDemoScreen>:
    GridLayout:
        rows: 3
        cols: 2
        Button:
            text:"Test"


        Button:
            text:"File"
            on_press: root.file_dialog()

        Button:
            text: 'Back'
            on_release: root.manager.current = 'menu'

我的部分 python 代码:

class RunDemoScreen(Screen):

    def file_dialog(self):
        Tk().withdraw()
        self.filename = askopenfilename()
        print(self.filename)

此代码中的所有内容都工作正常。

您需要创建一个图像小部件,然后使用您分配给它的 ID 将图像的来源更改为您的 self.filename 变量

在您的 .kv 中:

Image:
    id: imageWidget
    source: ''
    opacity: 0 # to make it completely invisible till your add a source file

在你的 .py 中:

def file_dialog(self):
    Tk().withdraw()
    self.filename = askopenfilename()
    imageWid = self.ids['imageWidget']
    imageWid.source = self.filename
    imageWid.opacity= 1 # to make it visible