簡體   English   中英

Python PyQt5:如何通過QFileDialog獲取圖像文件的名稱和內容

[英]Python PyQt5: How to get the name and the content of an image file by QFileDialog

如何使用QFileDialog打開圖像文件? 我也想知道如何獲取文件的名稱,例如,圖像文件名為“ photo.png”,我想打印其名稱。

def openImage(self):
    fileName = QFileDialog(self)
    fileName.getOpenFileName()
    image = QImage(fileName)
    print(fileName.FileName)

我自己解決問題:

def openImage(self):
    fileName = QFileDialog().getOpenFileName()
    filePath = str(fileName[0])

    image = QImage(filePath)

    label = QLabel()
    label.setPixmap(QPixmap().fromImage(image))

    self.addWidget(label, 0, 0)

    fileObject = filePath.split('/')
    file = fileObject[ len(fileObject) - 1 ]
    print(file)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM