PySide2无法显示图像

问题描述 投票:0回答:1

我尝试使用pyside2 Qlabel和Qpixmap在新窗口上显示图像,但是图像没有显示,窗口完全是空的。我在Internet上搜索了解决方案,并复制了其他人的成功脚本来检查这是否是我的计算机本身的问题,在这里我复制了以下代码from here并在计算机上运行它。如果导入的软件包是PyQt5,则可以使用!但是,如果我将PyQt5替换为PySide2,它将无法再次使用,有人可以测试代码吗?谢谢!

import sys
from PySide2.QtGui import QPixmap
from PySide2.QtWidgets import QMainWindow, QApplication, QLabel

class MainWindow(QMainWindow):

    def __init__(self):
        super(MainWindow, self).__init__()
        self.title = "Image Viewer"
        self.setWindowTitle(self.title)

        label = QLabel(self)
        pixmap = QPixmap('cat.jpg')
        label.setPixmap(pixmap)
        self.setCentralWidget(label)
        self.resize(pixmap.width(), pixmap.height())


app = QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9tRW1wbS5wbmcifQ==” alt =“我的情况的屏幕截图”>

python pyside2
1个回答
0
投票

我找到正确的解决方案here那是:

这是您需要做的(相关主题:一,二)

  1. 在脚本所在的文件夹中创建“ imageformats”文件夹(例如,C:\ PyProgs \ imageformats)
  2. 从我的PySide“ imageformats”文件夹(C:\ Python27 \ Lib \ site-packages \ PySide \ plugins \ imageformats中复制dll机器)到创建的文件夹
  3. 将此代码添加到您的main()函数:

    路径= r“ C:\ PyProgs”

    app.addLibraryPath(path)

完整的主代码:

def main():

    app = QtGui.QApplication(sys.argv)
    path = r"C:\PyProgs"
    app.addLibraryPath(path)

    ex = Example()
    sys.exit(app.exec_())
© www.soinside.com 2019 - 2024. All rights reserved.