Icons and images in PyQt5 do not display under Ubuntu 18.04 - python

I installed PyQt5 from Pypi on Ubuntu 18.04 desktop and am attempting to work my way through a tutorial. Most things work fine except when icons or images are to be displayed. For icons, the space needed to display it is reserved and you can interact with the space as if the icon image was there but no image displays.
For images, the space is reserved with the correct dimensions but no image is displayed.
This implies to me that PyQt5 is finding the icons and images and is doing everything except actually displaying them. A sample script is included here:
import sys
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QLabel
class Example(QWidget):
def __init__(self) -> None:
super().__init__()
self.initUI()
def initUI(self) -> None:
hbox = QHBoxLayout(self)
pixmap = QPixmap('WiseOldBirdLogo.png')
lbl = QLabel(self)
lbl.setPixmap(pixmap)
hbox.addWidget(lbl)
self.setLayout(hbox)
self.move(300, 200)
self.setWindowTitle('Pixmap')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
Can anyone suggest what I need to do to get the icons and images to display.

Related

Images from network drive wont show when python script converted to exe

I wrote small program to display images from network drive. It works perfectly when is launched as .py,
but when converted to .exe file (using auto-py-to-exe as one directory) program starts but not displays images and returns error "QPixmap::scaled: Pixmap is a null pixmap".
I have already tried:
path = "\\\\ow-sql\\Users\\file scanner\\Documents\\job_scans_copy\\20206\\012252988_30-06-2020-
151531.jpg"
path = r"\\" + os.path.join("ow-sql", "Users", "file scanner", "Documents", "job_scans_copy","20206","012252988_30-06-2020-151531.jpg")
I also tried both \ and /.
I can't simply include file into the build as the files will change and new files will be added.
There is no problem with images from img. folder.
for example:
img = QPixmap("img\\image.jpg")
works perfect as an exe.
Would you advise me how to fix this problem?
Please find simplified code below:
import sys
from PyQt5 import QtCore
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget, QPushButton, QLabel, QScrollArea
from PyQt5.QtGui import QPixmap
class ApplicationWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
main_widget = QWidget(self)
btn = QPushButton("Close", self)
btn.clicked.connect(self.close)
img = QPixmap("\\\\ow-sql\\Users\\file scanner\\Documents\\job_scans_copy\\20206\\012252988_30-06-2020-151531.jpg")
label = QLabel(main_widget)
pixmap_resized = img.scaled(200, 200)
label.setPixmap(pixmap_resized)
scrollArea = QScrollArea(main_widget)
scrollArea.setWidgetResizable(True)
scrollArea.setWidget(label)
l = QVBoxLayout(main_widget)
l.addWidget(scrollArea)
l.addWidget(btn)
self.setCentralWidget(main_widget)
def closeEvent(self, ce):
self.close()
if __name__ == '__main__':
app = QApplication(sys.argv)
aw = ApplicationWindow()
aw.show()
app.exec_()
I resolved my problem simply by using cx_Freeze to create an executable and everything works perfect.
For some reason pyInstaller does not want to load the image into QLabel if it's not included in build. It doesn't matter if it's a path to the file or the file is loaded as a BLOB from the database. None of them works.
cx_Freeze solved the problem without any additional code changes.

Why does QFontDialog.getFont() always return the same font?

Every time I use QFontDialog.getFont() to get a font, it returns the same QFont object.
font, ok = QFontDialog.getFont()
print(font)
if ok:
self.lbl.setFont(font)
print(self.lbl.font())
Above is the code segment, and the result is below (I make different choices but the font of the QLabel and the font I get from getFont() is always the same one).
/Users/yao/PycharmProjects/QT_test/venv/bin/python /Users/yao/PycharmProjects/QT_test/test/4_2.py
<PyQt5.QtGui.QFont object at 0x10b428748>
<PyQt5.QtGui.QFont object at 0x10b4287b8>
<PyQt5.QtGui.QFont object at 0x10b428748>
<PyQt5.QtGui.QFont object at 0x10b4287b8>
Update:
Below is some information about my environment:
macOS Mojave
PyQt5
Python 3.6
I have tested the codes by #ekhumoro and the results are strange:
BEFORE:
string:.SF NS Text, 13,-1,5,50,0,0,0,0,0
family:.SF NS Text
size:13
AFTER:
string:.SF NS Text, 13,-1,5,50,0,0,0,0,0
family:.SF NS Text
size:13
UPDATE:
This seems to be caused by a bug that only affects macOS: see QTBUG-6071 and QTBUG-69878. This should have been fixed in Qt-5.12, so you need to make sure you've installed the latest versions of both Qt5 and PyQt5 in order to resolve this issue.
Below is a test script with some screenshots of the output I get on Linux using Qt-5.12.0 with PyQt- 5.11.3. As you can see, everything works as expected. If you get different behaviour, you should edit your question and state the exact versions of Qt and PyQt you are using, which platform you are testing on, and show some sample output from the test script.
Test Script:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Window(QtWidgets.QWidget):
def __init__(self):
super(Window, self).__init__()
self.button = QtWidgets.QPushButton('Change Font')
self.button.clicked.connect(self.updateLabel)
self.label = QtWidgets.QLabel()
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(self.label)
layout.addWidget(self.button)
self.updateLabel()
def updateLabel(self):
if self.label.text():
new = QtWidgets.QFontDialog.getFont()[0]
else:
new = QtGui.QFont(self.label.font())
before = self.label.font()
self.label.setFont(new)
after = self.label.font()
text = []
for font in before, after:
info = QtGui.QFontInfo(font)
text.append('BEFORE:' if font is before else 'AFTER:')
text.append(' string: %s' % font.toString())
text.append(' family: %s' % info.family())
text.append(' size: %s' % info.pointSize())
text.append('')
text = '\n'.join(text)
self.label.setText(text)
print(text)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.setGeometry(800, 100, 500, 100)
window.show()
sys.exit(app.exec_())
Output:
<PyQt5.QtGui.QFont object at 0x10b4287b8> only gives you the location in memory of your object, it's basically a memory cell number, but in hexadecimal. You can have a different object (here, a different QFont object), stored in the same memory space.
You could check that the QFont object here is the one you selected by doing this:
font, ok = QFontDialog.getFont()
print(font.toString())
Update: Could you try the following code and tell us what you get?
import sys
from PyQt5.QtGui import QFont
from PyQt5.QtWidgets import QLabel, QApplication, QDialog, QVBoxLayout
app = QApplication(sys.argv)
dlg = QDialog()
layout = QVBoxLayout()
for fontname in ["Verdana", "Arial", "Consolas"]:
widget = QLabel(fontname)
font = QFont(fontname)
widget.setFont(font)
layout.addWidget(widget)
dlg.setLayout(layout)
dlg.show()
r = app.exec_()

PyQt icons missing?

I am new to PyQt and I am learning to make GUIs based on online tutorials I found. One of the examples in the tutorials uses an icon, here is the code from the tutorial:
import sys
from PyQt5.QtWidgets import QMainWindow, QAction, qApp, QApplication
from PyQt5.QtGui import QIcon
class Example(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
exitAct = QAction(QIcon('exit24.png'), 'Exit', self)
exitAct.setShortcut('Ctrl+Q')
exitAct.triggered.connect(qApp.quit)
self.toolbar = self.addToolBar('Exit')
self.toolbar.addAction(exitAct)
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('Toolbar')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
The output according to the tutorial should be
But for me it's this
As I said, I just started with PyQt and I just installed PyQt through pip
pip install PyQt5
I'm using Python3.6 and PyQt5. Any help is greatly appreciated!
Yes, PyQt5 does come with a list of default icons. You can find them here:
List of PyQt Icons
However, it seems the "exit" icon from the tutorial you refrenced used a local icon downloaded on their computer. You need to download the same icon and name it 'exit24.png' next to your python file.

Python 3.6 | PyQt5 | QLabel doesn't display QPixmap

I'm trying to display a PNG image in a Widget using QLabel. I built the Widget using Qt Designer and the label shows the image properly. When I load the .ui into mi .py and execute it, everything shows up well, except for the label. I tried loading the QPixmap manually on the python code, but it didn't work either.
from PyQt5.QtWidgets import (QApplication, QLabel)
from PyQt5.QtGui import (QPixmap)
from PyQt5 import uic
import sys
import os
form = uic.loadUiType('gui/Files/Login.ui')
class LoginWindow(form[0], form[1]):
def __init__(self):
super().__init__()
self.setupUi(self)
self.logoLabel.setPixmap(QPixmap(os.path.abspath(os.path.join(os.path.dirname(__file__), 'logo.png'))))
self.logoLabel.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
win = LoginWindow()
win.show()
app.exec_()
Any idea what am I doing wrong? Any sugestions on how could I display the image other than with QLabel?

Creating a transparent overlay with qt

I've been learning python recently and now I wanted to (try to) create my first real application, a subtitle player for Linux. So far I've been using the Greenfish subtitle player, which is aimed at Windows users and not properly working in Linux.
I wanted to create the application in qt, since I discovered that transparent windows are not possible in tkinter, but if anybody knows a better framework please suggest!
Now before starting I've been researching the web for several hours to discover how to get my application to show over a full screened flash video and it seems like this is not possible. However the aforementioned GF subtitle player manages to do so in Windows, but not in Linux(maybe it's also because it's running through wine).
So my question is it possible to create a transparent application that remains over a fullscreened flash video and if so, could you point me in the right direction?
Thanks in advance.
edit:
here some example code I've been trying. The window produced by this piece of code does not stay above a fullscreened video
import sys
from PyQt4 import QtGui, QtCore
class mymainwindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self, None, QtCore.Qt.WindowStaysOnTopHint)
app = QtGui.QApplication(sys.argv)
mywindow = mymainwindow()
mywindow.show()
app.exec_()
mywindow.show()
Update for PyQt5 pip install PyQt5
import sys
from PyQt5 import QtGui, QtCore, uic
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QMainWindow, QApplication
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setWindowFlags(
QtCore.Qt.WindowStaysOnTopHint |
QtCore.Qt.FramelessWindowHint |
QtCore.Qt.X11BypassWindowManagerHint
)
self.setGeometry(
QtWidgets.QStyle.alignedRect(
QtCore.Qt.LeftToRight, QtCore.Qt.AlignCenter,
QtCore.QSize(220, 32),
QtWidgets.qApp.desktop().availableGeometry()
))
def mousePressEvent(self, event):
QtWidgets.qApp.quit()
if __name__ == '__main__':
app = QApplication(sys.argv)
mywindow = MainWindow()
mywindow.show()
app.exec_()
The example code below will create a centred, frameless window that should stay on top of all other windows on Linux (you can click on the window to close it).
import sys
from PyQt4 import QtGui, QtCore
class mymainwindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.setWindowFlags(
QtCore.Qt.WindowStaysOnTopHint |
QtCore.Qt.FramelessWindowHint |
QtCore.Qt.X11BypassWindowManagerHint
)
self.setGeometry(QtGui.QStyle.alignedRect(
QtCore.Qt.LeftToRight, QtCore.Qt.AlignCenter,
QtCore.QSize(220, 32),
QtGui.qApp.desktop().availableGeometry()))
def mousePressEvent(self, event):
QtGui.qApp.quit()
app = QtGui.QApplication(sys.argv)
mywindow = mymainwindow()
mywindow.show()
app.exec_()

Categories

Resources