PyQt5 does now show icons?

匿名 (未验证) 提交于 2019-12-03 00:56:02

问题:

I just installed python3 (3.5.2) and Pyqt5(5.8.2) and i am following this tutorial to learn and make an GUI:

http://zetcode.com/gui/pyqt5/firstprograms/

I'm trying to run 2nd example but program is given an error (which also happened on the 1st one, but since it had no image i took no notice) which is the following:

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 is the meaning of this? Am i missing some packages?

I installed pyqt first with command,

sudo -H pip3 install PyQt5 but python3 was not acnkoledging its existence so i searched the apt ubuntu repos and installed with sudo apt install python3-PyQt5

I also tried to reference the image by full path /foo/bar/image.png and nothing

What is the problem?

EDIT - 1

The code that i am 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 rechecked all my packages (unistalled all pyqt and installed again). The error is slightly different but the result is the same

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

Screencapture:

回答1:

Notice that you are having no icons at all for all applications, not just for the PyQt icon example. This is because by default, certain environments turn off the icons in the titlebar. You have to enable them. In Settings/Settings Edidor select xfwm4. Find the show_app_icon option and check it. Change a theme back and forth to see the changes; they are not visible right away.

After this, you will see the icon in the titlebar of the PyQt5 example.

As for the warning; it is a recent thing and it has to do something with the incopatibilities between Qt and GTK theming. I have not found a solution to remove the warning so far.



回答2:

So first off, you have no errors in your code. That's more akin to a warning but not even. What the following line is telling you

QApplication: invalid style override passed, ignoring it 

is that your style option is invalid. If that were an error your script wouldn't run at all.

What I see right off the bat is this, you never supply a path to your image.

Now if the image is in the same root directory as the script then it should recognize said image without a path. But if you're attempting to do what I think you are it wouldn't work like that anyway. I think you're trying to create a launcher icon as well as a title bar icon, which typically goes hand in hand.

It appears to me that you've added it to Atom as some form of resource file. In which case most Ide's create a path for that file. Sometimes it's a path, other times a local url. QT its self does both when working with the QT creator.

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

What I can say is this. you're using Linux which means .ico files are useless. I told you before linux doesn't handle icon files the same way windows does. This is most likely your problem.

So I sugesst 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 that take a look at this if you have to https://help.ubuntu.com/community/UnityLaunchersAndDesktopFiles#Using_a_text_editor

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

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

I hope this helps you out!



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!