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

2024-07-04 05:21:16 发布

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

我正在为我的应用程序构建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)

此代码中的所有内容都正常工作。你知道吗


Tags: the代码textin图像self应用程序on
1条回答
网友
1楼 · 发布于 2024-07-04 05:21:16

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

在您的.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

相关问题 更多 >

    热门问题