import sys
from PySide6.QtWidgets import QApplication, QLabel
app = QApplication(sys.argv)
label = QLabel("<font color=red size=40>Hello World!</font>")
# label.show()
app.exec()
What actually happens if you do not call label.show()?
I see that the window does not appear but . . .
Is there a way to direct a "close window event" to the non-shown window?
Related
Not much info I've found, but looks like this snippet should works:
import sys
from PySide2.QtWidgets import QApplication, QMenu, QPushButton, QSystemTrayIcon, QWidgetAction
app = QApplication(sys.argv)
menu = QMenu()
button = QPushButton("yoba")
action = QWidgetAction(menu)
action.setDefaultWidget(button)
menu.addAction(action)
menu.addAction("Quit").triggered.connect(sys.exit)
tray = QSystemTrayIcon()
tray.setContextMenu(menu)
tray.show()
sys.exit(app.exec_())
But, I see only Quit item and empty item above, no push button appear. So, the question is "how to add custom widgets to tray menu?"
Ok, looks like this is a bug: https://bugreports.qt.io/browse/QTBUG-26840
Since if call menu.show() instead of tray.show() everything is fine.
I would like my application to consist only of a pop-up menu that opens immediately as soon as it is run. I attempted this solution but nothing appears when I run it:
#!/usr/bin/env python3
from PyQt5.QtCore import (Qt, QPoint)
from PyQt5.QtGui import QCursor
from PyQt5.QtWidgets import (QApplication, QMenu, QAction)
def clicked():
print("CLICKED")
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
menu = QMenu()
menu.addAction(QAction("&Click me", triggered=clicked))
menu.exec_(QCursor().pos())
sys.exit(app.exec_())
This will display a simple popup window with a single option, that calls the clicked function on click:
from PySide2.QtGui import QCursor
from PySide2.QtWidgets import QApplication, QMenu
def clicked():
print("CLICKED")
if __name__ == '__main__':
app = QApplication()
menu = QMenu()
menu.addAction("Click me", clicked)
menu.exec_(QCursor().pos())
Or if you want to use QAction still, then move its definition outside:
from PySide2.QtGui import QCursor
from PySide2.QtWidgets import QApplication, QMenu, QAction
def clicked():
print("CLICKED")
if __name__ == '__main__':
app = QApplication()
menu = QMenu()
action = QAction("&Click me", triggered=clicked)
menu.addAction(action)
menu.exec_(QCursor().pos())
As pointed out by ekhumoro in the comment below:
Qt does not take ownership of actions added via addAction. You must
keep an explicit reference to them, otherwise they will get
garbage-collected.
Note I am using PySide2, but it shouldn't change anything.
Okay I am not certain how minimalistic you want this Menu to be but this is a more static menu (aka it sticks around) with more than one option in case you are trying to build some kind of Menu driven Windowing platform or just a Menu of commands. Also it being a class allows it to be importable to whatever you might be developing large scale. Further you can expand the menu by adding additional menu items with different actions and lastly it follows basic pyqt programming methodologies
from sys import exit as sysExit
from PyQt5.QtWidgets import QApplication, QAction, QWidget, QHBoxLayout, QMenuBar
class MainMenu(QWidget):
def __init__(self):
QWidget.__init__(self)
self.setMaximumSize(self.minimumSize())
self.Click1Act = QAction("&Click Me 1", triggered=self.Clicked1)
self.Click2Act = QAction("&Click Me 2", triggered=self.Clicked2)
self.SimpleMenu = QMenuBar()
self.ClickMenu = self.SimpleMenu.addMenu('Clickables')
self.ClickMenu.addAction(self.Click1Act)
self.ClickMenu.addSeparator()
self.ClickMenu.addAction(self.Click2Act)
HBox = QHBoxLayout()
HBox.addWidget(self.SimpleMenu)
self.setLayout(HBox)
def Clicked1(self):
print("CLICKED ONE")
def Clicked2(self):
print("CLICKED TWO")
if __name__ == "__main__":
MainThred = QApplication([])
MainGui = MainMenu()
MainGui.show()
sysExit(MainThred.exec_())
When I run the basic script:
import sys
from PySide2.QtWidgets import QApplication, QLabel
app = QApplication(sys.argv)
label = QLabel("Hello World")
label.show()
app.exec_()
forthe first time it all works fine. However, if I run it a second time I get:
File "../script.py", line 17, in <module>
app = QApplication(sys.argv)
RuntimeError: Please destroy the QApplication singleton before creating a new QApplication instance.
I am running the scripts in an Ubuntu machine. I get the same error in python2 and python3.
Thanks !
Probably your IDE has already created a QApplication, so the solution is to create a QApplication if it does not exist:
app = QApplication.instance()
if app is None:
app = QApplication(sys.argv)
I'm begining with Python. I'm developing a simple desktop app to print a control check of motorcycles services. The problem is that the UI not showing the unchecked checkboxes in the table when I run the script
Screenshot with errors in red
Screenshot with cells configuration in Qt5 Designer
The code
import sys
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QApplication, QDialog
from PyQt5.uic import loadUi
class ServiceLabeler(QDialog):
def __init__(self):
super(ServiceLabeler, self).__init__()
loadUi('main.ui', self)
self.btnPrint.clicked.connect(self.printLabel)
#pyqtSlot()
def printLabel(self):
print('Printing ...')
app = QApplication(sys.argv)
widget = ServiceLabeler()
widget.show()
sys.exit(app.exec_())
If you want to see the main.ui code please follow next link to gist
https://gist.github.com/CristalT/0d2e5cc2c684c6dc2b87bd5ec1d7348e
How to add image/icon with text in a qlistwidget in pyqt4 python? I want to add an icon with text just like a chat system. thanks
I have tried this right now and it works, supposing you have a file named tick.png in the same folder as this script.
import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import QApplication, QDialog, QListWidgetItem, QListWidget, QIcon
def main():
app = QtGui.QApplication(sys.argv)
window = QDialog()
list = QListWidget( window )
itm = QListWidgetItem( "Tick" );
itm.setIcon(QIcon(r"tick.png"));
list.addItem(itm);
window.show( )
sys.exit(app.exec_())
if __name__ == '__main__':
main()
The chat-like-icon system may be different from this, but right now I don't see a way to have a QListWidgetItem with multiple smileys and text.
You may think of smileys as a particular case of a QListWidgetItem where the text is blank and only the icon is present.
Another solution is using a read-only QTextEdit as chatboard and have the user typing its text + icon + text (etc.) in a separate editable QTextEdit. Then, when he presses the send button, append everything he typed to the read-only QTextEdit.
import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import QApplication, QDialog, QListWidgetItem, QListWidget, QIcon, QTextEdit, QTextDocumentFragment
def main():
app = QtGui.QApplication(sys.argv)
window = QDialog()
list = QListWidget( window )
textEditor = QTextEdit( window );
textEditor.setReadOnly( True )
tick_icon = QTextDocumentFragment.fromHtml(r"<img src='tick.png'>");
textEditor.insertPlainText ( " ValiumKnight writes: " )
textEditor.textCursor().insertFragment(tick_icon);
textEditor.insertPlainText ( " Hello World " )
textEditor.textCursor().insertFragment(tick_icon);
textEditor.textCursor().insertFragment(tick_icon);
textEditor.textCursor().insertFragment(tick_icon);
window.show( )
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Bye!