LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-29-2018, 11:17 AM   #1
vysero
Member
 
Registered: May 2018
Posts: 137

Rep: Reputation: Disabled
Trouble with PyQt4 and image displaying


Hey guys, I am attempting to display an image with this setWindowIcon command. However, I am getting an error. Here is my attempt:

Code:
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()?
 
Old 06-29-2018, 12:04 PM   #2
vysero
Member
 
Registered: May 2018
Posts: 137

Original Poster
Rep: Reputation: Disabled
Well this is not the right method. I did manage to open the file. The problem is I can't use a string. I need the pngdata to actually be a jpg or png file because that is the type that QIcon is expecting. Anyone know how to open a jpg or png so that it stays as a jpg or png and does not get converted into a string?
 
Old 06-29-2018, 12:05 PM   #3
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,240

Rep: Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322
Code:
f.read()
This is where it's failing, right?

It's reading the file in text mode, not binary mode.

You want to precede that with:

Code:
with open('Logo.png', 'rb') as f:
To open the file for reading in binary mode.

See:

https://docs.python.org/3.3/tutorial...-writing-files

Last edited by dugan; 06-29-2018 at 12:07 PM.
 
Old 06-29-2018, 12:29 PM   #4
vysero
Member
 
Registered: May 2018
Posts: 137

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by dugan View Post
Code:
f.read()
This is where it's failing, right?

It's reading the file in text mode, not binary mode.

You want to precede that with:

Code:
with open('Logo.png', 'rb') as f:
To open the file for reading in binary mode.

See:

https://docs.python.org/3.3/tutorial...-writing-files
Right, yes I figured this out eventually. Unfortunately, I ran into another problem which I attempted to convey with my last post. Here is my altered version of the script:

Code:
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")

    self.setWindowIcon(QtGui.QIcon('TeeJetLogo.jpg'))
    self.show()

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

sys.exit(app.exec_())
The program runs just fine now but the image is not being displayed. I can not seem to figure out why it wont be displayed. This seems fairly straightforward but apparently that is not the case.
 
Old 06-29-2018, 07:31 PM   #5
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
Your code displays an icon in the upper left corner of the title bar on my box. With a different jpeg obviously.

Is the image file in the same directory as your code?

If you want to display the image in the window, you'll have to do something like this:
Code:
...
    self.setWindowTitle("PyQt4 Window")
    self.label = QtGui.QLabel(self)
    self.label.setPixmap(QtGui.QPixmap('TeeJetLogo.jpg'))
    self.setCentralWidget(self.label)
    self.show()
...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Image not displaying on web page jfraymondpa Linux - Server 11 03-16-2011 12:56 PM
grabbing data and displaying an image eoc92866 Programming 9 11-07-2010 07:02 AM
grabbing data and displaying an image eoc92866 Linux - Newbie 4 11-04-2010 11:15 PM
displaying image files using QT or a slideshow mist99 Linux - Newbie 6 01-28-2009 11:54 PM
Help with displaying an image in PHP pandersson61 Programming 3 07-31-2008 09:40 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration