问题
I am new to pyside and I am following a tutorial at http://zetcode.com/gui/pysidetutorial/widgets2/. I am trying to get an image to show in a window. It seems that I should be able to pass in a file location for a picture on my computer, but no matter what I do I can't seem to get it to display the file I pass in. Any help would be greatly appreciated. Thanks!
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:
Here's what you need to do (related topics: one, two)
- Create
"imageformats"
folder in the same folder where your script is located (for example,C:\PyProgs\imageformats
) - Copy
dlls
from PySide"imageformats"
folder (C:\Python27\Lib\site-packages\PySide\plugins\imageformats
on my machine) to created folder - Add this code to your
main()
function:
path = r"C:\PyProgs" app.addLibraryPath(path)
Full main
code:
def main():
app = QtGui.QApplication(sys.argv)
path = r"C:\PyProgs"
app.addLibraryPath(path)
ex = Example()
sys.exit(app.exec_())
来源:https://stackoverflow.com/questions/24346038/pyside-qpixmap-not-working