0

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()
Dair
  • 15,910
  • 9
  • 62
  • 107
calthoff
  • 386
  • 1
  • 4
  • 14
  • Your code exactly as it is works fine for me. So you must have a very specific problem! You can test with `isNull()` if the image was found and loaded but this does not explain why it wasn't loaded. – NoDataDumpNoContribution Jun 23 '14 at 09:37
  • Thanks, isNull() helped and you were right my environment was messed up, thanks. – calthoff Jun 24 '14 at 20:38

1 Answers1

0

Here's what you need to do (related topics: one, two)

  1. Create "imageformats"folder in the same folder where your script is located (for example, C:\PyProgs\imageformats)
  2. Copy dlls from PySide "imageformats" folder (C:\Python27\Lib\site-packages\PySide\plugins\imageformats on my machine) to created folder
  3. 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_())
Community
  • 1
  • 1
NorthCat
  • 9,643
  • 16
  • 47
  • 50