Pyside Qpixmap 无法使用

0 投票
1 回答
1638 浏览
提问于 2025-04-18 10:38

我刚开始学习pyside,正在看一个教程,地址是 http://zetcode.com/gui/pysidetutorial/widgets2/。我想在窗口中显示一张图片。看起来我应该能把电脑上图片的文件位置传进去,但无论我怎么做,似乎都无法显示我传入的文件。任何帮助都会非常感激。谢谢!

import sys
from PySide import QtGui, QtCore

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):      

        hbox = QtGui.QHBoxLayout(self)
        pixmap = QtGui.QPixmap("C://Users//Public//Pictures//Sample Pictures//Desert.jpg")

        lbl = QtGui.QLabel(self)
        lbl.setPixmap(pixmap)

        hbox.addWidget(lbl)
        self.setLayout(hbox)

        self.setGeometry(300, 300, 280, 170)
        self.setWindowTitle('Red Rock')
        self.show()        

def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

1 个回答

0

你需要做的事情如下(相关话题:,

  1. 在你的脚本所在的文件夹里创建一个叫 "imageformats" 的文件夹(比如说,C:\PyProgs\imageformats
  2. 把 PySide 的 "imageformats" 文件夹里的 dlls 复制到你刚创建的文件夹里(在我电脑上,这个文件夹的路径是 C:\Python27\Lib\site-packages\PySide\plugins\imageformats
  3. 在你的 main() 函数里添加以下代码:
path = r"C:\PyProgs"
app.addLibraryPath(path)

完整的 main 代码:

def main():

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

    ex = Example()
    sys.exit(app.exec_())

撰写回答