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.
Related
I am currently exploring the possibilities of building a GUI that can directly interact with and display PowerPoint-presentations. For that I am using Python and PyQt5 on a windows machine with PowerPoint installed. The PowerPoint-presentation is embedded inside a QAxWidget and generally works as intended.
Unfortunately there is a small but hugely annoying bug that I haven't yet found a solution for. When the focus is not set on the QAxWidget, the content disappears until I click into the QAxWidget again.
Here's a gif that showcases this by clicking into other GUI elements:
I've already learned about setFocus(), but that doesn't seem to be applicable here.
Also I've looked into a couple of Qt-development-books for Python and C++, but I haven't found a single one working with ActiveX controls and QAxWidgets. Any research material working with QAxWidgets, ActiveX, anything that might suit my needs, is also highly appreciated. Only research material I am aware of is the C++ reference and posts on forums.
With the following minimal example code and the requirements stated in the beginning, you should be able to reproduce what I've shown in the GIF:
import sys
from PyQt5.QAxContainer import QAxWidget
from PyQt5.QtCore import QEvent
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QPushButton, QApplication, QMainWindow
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.menuBar().addMenu("&About")
self.centralwidget = QWidget(self)
self.centralwidget_vlayout = QVBoxLayout(self.centralwidget)
self.axwidget = QAxWidget(self.centralwidget)
self.axwidget.installEventFilter(self)
self.axwidget.setControl(r"C:\path\to\presentation.pptx")
self.pushbutton = QPushButton(self.centralwidget)
self.pushbutton.setText("Press me!")
self.centralwidget_vlayout.addWidget(self.axwidget)
self.centralwidget_vlayout.addWidget(self.pushbutton)
self.setCentralWidget(self.centralwidget)
self.show()
def eventFilter(self, widget: QWidget, event: QEvent):
if event.type() == QEvent.Resize and widget is self.axwidget:
self.axwidget.setFixedHeight(int(self.axwidget.width() / 16 * 9))
return super(MainWindow, self).eventFilter(widget, event)
if __name__ == '__main__':
app = QApplication(sys.argv)
mainWin = MainWindow()
sys.exit(app.exec())
Now to the main question: How can I make sure that the PowerPoint-presentation is visible at all times, even when the focus is set to another GUI-element?
I'd like to use Qt's virtual keyboard (Qt5.11.1 or newer?) in my python3 (3.6) project on Win10, but I'm stuck.
I have anaconda environment and I'm quite sure the Virtual keyboard is also somewhere in there, because I can find some folders with correct name.
What should the main program look like, besides the obvious, to virtual keyboard pop up when an input widget gets selected?
import sys
from qtpy.QtWidgets import QApplication, QDialog, QVBoxLayout, QSpinBox
class Test(QDialog):
def __init__(self):
super(Test, self).__init__()
layout = QVBoxLayout()
self.setLayout(layout)
for i in range(2):
layout.addWidget(QSpinBox())
if __name__ == '__main__':
import os
os.environ["QT_IM_MODULE"] = "qtvirtualkeyboard"
app = QApplication(sys.argv)
dialog = Test()
sys.exit(dialog.exec())
edit: to be clear, i don't want to reinvent or customize the wheel, just use it.
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.
I am learning pyqt5 and somehow I can't use fcitx in the text box created by QTextEdit or QLineEdit although fcitx works normally with other Qt apps like goldendict or kate. But later I found out that fcitx also doesn't work with another Qt app named Retext which uses Qt 5.10. Maybe this has something to do with the latest version of Qt or so I think.
Here's my code, just a simple textbox and nothing else:
import PyQt5.QtWidgets as QtWidgets
import sys
class App(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.text = QtWidgets.QTextEdit()
self.initUI()
def initUI(self):
vbox = QtWidgets.QVBoxLayout()
vbox.addWidget(self.text)
self.setLayout(vbox)
self.show()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
Thanks to #ekhumoro, I know how to fix this. Just enable input method by adding this line to the __init__ function:
self.setAttribute(Qt.WA_InputMethodEnabled)
Then do this:
cp /usr/lib/x86_64-linux-gnu/qt5/plugins/platforminputcontexts/fcitxplatforminputcontextplugin.so ~/.local/lib/python3.6/site-packages/PyQt5/Qt/plugins/platforminputcontexts
sudo chmod +x ~/.local/lib/python3.6/site-packages/PyQt5/Qt/plugins/platforminputcontexts/fcitxplatforminputcontextplugin.so
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_()