Python Forum
Trouble displaying an image in PyQt4
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trouble displaying an image in PyQt4
#1
Hey guys, I am attempting to display an image with this setWindowIcon command. However, I am getting an error. Here is my attempt:

import sys
from PyQt4 import QtGui

class Window(QtGui.QMainWindow):
  def __init__(self):
    super(Window, self).__init__() #returns parent object (q main window object)
    self.setGeometry(50, 50, 500, 300)
    self.setWindowTitle("PyQt4 Window")

    with open('Logo.png') as f:
      pngdata = f.read()

    self.setWindowIcon(QtGui.QIcon(pngdata))
    self.show()

app = QtGui.QApplication(sys.argv)
GUI = Window()

sys.exit(app.exec_())
The error:

/usr/bin/python3 /home/rob/PycharmProjects/untitled/xcfgh.py
Traceback (most recent call last):
File "/home/rob/PycharmProjects/untitled/xcfgh.py", line 17, in <module>
GUI = Window()
File "/home/rob/PycharmProjects/untitled/xcfgh.py", line 11, in __init__
pngdata = f.read()
File "/usr/lib/python3.5/codecs.py", line 321, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte

I am a bit confused. Why would I be getting a utf-8 error this is an image not a string. Should I not be using f.read()?
Reply
#2
an image is binary data, so to read:
with open('Logo.png', 'rb') as f:
Reply
#3
You can open the image as a QPixmap instead;
import sys
from PyQt4 import QtGui

class Window(QtGui.QMainWindow):
  def __init__(self):
    super(Window, self).__init__() #returns parent object (q main window object)
    self.setGeometry(50, 50, 500, 300)
    self.setWindowTitle("PyQt4 Window")

    pixmap = QtGui.QPixmap('/home/will/DATA/Images/12384249_501942289985392_374306926_n.gif')
    self.setWindowIcon(QtGui.QIcon(pixmap))
    self.show()

app = QtGui.QApplication(sys.argv)
GUI = Window()
sys.exit(app.exec_())
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Displaying Image Files in a GUI vman44 1 1,778 Mar-02-2020, 02:18 AM
Last Post: Larz60+
  [PyQt] PyQt4 dynamic QComboBox littleGreenDude 4 5,677 Jan-02-2019, 07:22 PM
Last Post: littleGreenDude
  [PyQt] PyQt4 handle dynamic checkbox click littleGreenDude 1 6,581 Dec-27-2018, 09:17 PM
Last Post: littleGreenDude
  PyQt4 installation frustration littleGreenDude 4 4,538 Dec-27-2018, 04:29 PM
Last Post: littleGreenDude
  [PyQt4] Is it right python coding scheme between TCP Server Thread and GUI class ? KimTom 3 3,248 Sep-18-2018, 01:21 PM
Last Post: Alfalfa
  How to Integrate PyQt4 Custom Widget with Qt Designer Zukias 1 3,912 Aug-29-2018, 05:33 PM
Last Post: Zukias
  Updating Python version from command prompt and Conversion from PyQt4 to PyQt5 Vysero 4 4,954 Jul-19-2018, 03:15 PM
Last Post: Vysero
  [Tkinter] Displaying an image that can't be closed nicochulo 4 3,607 Feb-15-2018, 10:27 AM
Last Post: nicochulo
  [PyQt] problem with PyQt4 jss15497 0 2,760 Jan-16-2018, 08:32 AM
Last Post: jss15497
  [PyQt] Learn pyqt5 using pyqt4 jimclay75051 5 5,767 Nov-13-2017, 06:29 AM
Last Post: zykbee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020