does not show icons

I just installed Python3 (3.5.2) and Pyqt5 (5.8.2) and I am following this tutorial to learn and create a GUI: http://zetcode.com/gui/pyqt5/firstprograms/

I'm trying to run the 2nd example, but the program returns an error (which also happened on the 1st, but since it had no image, I didn't notice), which is as follows:

QApplication: invalid style override passed, ignoring it.
No XVisualInfo for format QSurfaceFormat(version 2.0, options QFlags<QSurfaceFormat::FormatOption>(), depthBufferSize -1, redBufferSize 1, greenBufferSize 1, blueBufferSize 1, alphaBufferSize -1, stencilBufferSize -1, samples -1, swapBehavior QSurfaceFormat::SwapBehavior(SingleBuffer), swapInterval 1, profile  QSurfaceFormat::OpenGLContextProfile(NoProfile))
Falling back to using screens root_visual.

      

What does this mean? Am I missing some packages?

I first installed pyqt using this command:

sudo -H pip3 install PyQt5

but Python3 did not recognize its existence, so I searched the ubuntu apt repositories and installed:

sudo apt install python3-PyQt5

I also tried to link to the image in full path /foo/bar/image.png and nothing

What is the problem?

EDIT # 1

The code I'm using is from example 2:

#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
ZetCode PyQt5 tutorial

This example shows an icon
in the titlebar of the window.

author: Jan Bodnar
website: zetcode.com
last edited: January 2015
"""

import sys
import os
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QIcon

base_dir = os.path.dirname(os.path.abspath(__file__))
os.chdir(base_dir)

class Example(QWidget):

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

        self.initUI()

    def initUI(self):

        self.setGeometry(300, 300, 300, 220)
        self.setWindowTitle('Icon')
        self.setWindowIcon(QIcon('image.png'))

        self.show()


if __name__ == '__main__':

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

      

After your post, I reinstalled all my packages. The error is slightly different, but the result is the same:

python3 example_02.py 
QApplication: invalid style override passed, ignoring it.

      

Screenshot: Screenshot of my result

+4


source to share


4 answers


Note that you don't have icons for all applications, not just for the PyQt icon example. This is because by default some environments turn off icons in the title bar. You must enable them.

For example, in the Xfce desktop environment, we can use xfce4-settings-editor

. In the preferences / settings editor select xfwm4

. Find the option show_app_icon

and check it. Change the theme back and forth to see the changes; they are not immediately visible.

Settings Editor



After that, you will see an icon in the title of the PyQt5 example.

Application icon

As for the warning; this is a recent thing and has to do with incompatibility between Qt and GTK themes. I haven't found a solution to remove the warning so far.

+10


source


So, first of all, you have no bugs in your code. It sounds more like a warning, but not even that. What the next line says:

QApplication: invalid style override passed, ignoring it

      

is that your style parameter is invalid. If it was an error, your script would not run at all.

What I see right off the bat is never, you never show the way to your image.

Now, if the image is in the same root directory as the script, then it should recognize the specified image without the path. But if you try to do what I think you are, it still won't work. I think you are trying to create a launcher icon as well as a title icon, which usually goes hand in hand.

It looks to me like you added it to Atom as part of the resource file form. In most cases, Ide creates a path for this file. Sometimes this is the path, the other is the local url. QT does it himself as when working with the creator of QT.

I've never used Atom, so I can't tell you how it works.



I can say this. you are using Linux which means .ico files are useless. I told you before Linux would not handle icon files the same way windows do. This is most likely your problem.

So I suggest you take a look at this https://askubuntu.com/questions/476981/how-do-i-make-a-desktop-icon-to-launch-a-program

After you read this take a look at this if you need https://help.ubuntu.com/community/UnityLaunchersAndDesktopFiles#Using_a_text_editor

Both of these links explain how to create a launcher icon for your program.

The following link explains how to set an icon on the menu bar (title bar) in your program. PyQt4 set windows taskbar icon

Hope this helps you!

+3


source


I am researching PyQt5 from the author who is asking this question and also I have a problem my icons cannot show, I am trying to find a way to catch it, what I am doing, I hope it works!

First, it's important that you use an absolute path to the icons, for example:

self.setWindowIcon(QIcon("F:/Workspace/PyQT5-Study/images/web.png"))

      

but that's not a good idea, so you can use the second way like this:

from PyQt5.QtCore import QFileInfo
# ...
def initUI(self):
    # ...
    root = QFileInfo(__file__).absolutePath()
    self.setWindowIcon(QIcon(root+'/images/web.png'))
    # or
    # import os
    # current_dir = os.path.dirname(os.path.realpath(__file__))
    # self.setWindowIcon(QIcon(os.path.join(current_dir, 'images/web.png')))
    # ...

      

Finally, if your icons cannot be displayed as well, you should check this icon if it is a legal icon .

In short, regular images are not limited, so they can store many images and transform easily, but icons have a specific size, color and more importantly, icons have transparency, that is, you can see the background, they have a border (not always straight) ... So you can use online web tools to convert your image and try again, it really helps me!

Also you should check the original icon format, make sure you never change it like .jpg to .png and others. This will create a problem! Wish you can solve this problem!

0


source


if you write an absolute path it will work very well. in windows:

self.setWindowIcon(QIcon("C://Users//%pcname//Desktop//1.png"))

      

0


source







All Articles