Skip to content

Instantly share code, notes, and snippets.

@Ja7ad
Last active December 11, 2020 13:45
Show Gist options
  • Save Ja7ad/60169e13b1adcbb0a90fbbf44bd68bd6 to your computer and use it in GitHub Desktop.
Save Ja7ad/60169e13b1adcbb0a90fbbf44bd68bd6 to your computer and use it in GitHub Desktop.
PyQt5 QMessageBox with Details
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QGridLayout, QWidget, QCheckBox, QSystemTrayIcon, \
QSpacerItem, QSizePolicy, QMenu, QAction, QStyle, qApp, QMessageBox, QPushButton
from PyQt5.QtCore import QSize
class MainWindow(QMainWindow):
"""
Сheckbox and system tray icons.
Will initialize in the constructor.
"""
check_box = None
tray_icon = None
# Override the class constructor
def __init__(self):
# Be sure to call the super class method
QMainWindow.__init__(self)
self.setMinimumSize(QSize(480, 80)) # Set sizes
self.setWindowTitle("System Tray Application") # Set a title
central_widget = QWidget(self) # Create a central widget
self.setCentralWidget(central_widget) # Set the central widget
grid_layout = QGridLayout() # Create a QGridLayout
central_widget.setLayout(grid_layout) # Set the layout into the central widget
grid_layout.addWidget(QLabel("Application, which can minimize to Tray", self), 0, 0)
# Add a checkbox, which will depend on the behavior of the program when the window is closed
self.check_box = QCheckBox('Minimize to Tray')
self.btn_check = QPushButton()
self.btn_check.setText("show Message")
grid_layout.addWidget(self.check_box, 1, 0)
grid_layout.addWidget(self.btn_check, 2, 0)
grid_layout.addItem(QSpacerItem(0, 0, QSizePolicy.Expanding, QSizePolicy.Expanding), 2, 0)
# Init QSystemTrayIcon
self.btn_check.clicked.connect(lambda: self.testmsg())
self.tray_icon = QSystemTrayIcon(self)
self.tray_icon.setIcon(self.style().standardIcon(QStyle.SP_ComputerIcon))
'''
Define and add steps to work with the system tray icon
show - show window
hide - hide window
exit - exit from application
'''
show_action = QAction("Show", self)
quit_action = QAction("Exit", self)
hide_action = QAction("Hide", self)
show_action.triggered.connect(self.show)
hide_action.triggered.connect(self.hide)
quit_action.triggered.connect(qApp.quit)
tray_menu = QMenu()
tray_menu.addAction(show_action)
tray_menu.addAction(hide_action)
tray_menu.addAction(quit_action)
self.tray_icon.setContextMenu(tray_menu)
self.tray_icon.show()
# Override closeEvent, to intercept the window closing event
# The window will be closed only if there is no check mark in the check box
def closeEvent(self, event):
if self.check_box.isChecked():
event.ignore()
self.hide()
self.tray_icon.showMessage(
"Tray Program",
"Application was minimized to Tray",
QSystemTrayIcon.Information,
2000
)
def testmsg(self):
msg = QMessageBox()
msg.setIcon(QMessageBox.Information)
msg.setWindowTitle("MyMessage")
msg.setText("Hello world")
msg.setInformativeText("this is more details")
msg.setDetailedText("details text")
msg.setStandardButtons(QMessageBox.Ok)
msg.exec_()
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
mw = MainWindow()
mw.show()
sys.exit(app.exec())
@Ja7ad
Copy link
Author

Ja7ad commented Dec 11, 2020

grid_layout = QGridLayout(self)

remove self

grid_layout = QGridLayout()

to prevent the error message

QLayout: Attempting to add QLayout "" to MainWindow "", which already has a layout

thanks do it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment