I have some strange trouble with PyQt5
application creating. Closing the window kills the whole application, though I don't need it, because I also have a tray icon. I am running python3 on Debian, though I don't think it matters.
There is a solution for C#. Close a windows form without exiting the entire application
Creating another form, but not displaying it. Is it OK for PyQt
?
import sys
import PyQt5.QtWidgets
class SettingsMenu(PyQt5.QtWidgets.QDialog):
def __init__(self, parent):
super().__init__()
self.setWindowTitle("Settings")
self.resize(300, 200)
class ContextMenu(PyQt5.QtWidgets.QMenu):
def __init__(self):
super().__init__()
self.settings_action = self.addAction('Settings')
self.settings_action.triggered.connect(self.display_settings_menu)
def display_settings_menu(self, event):
self.settings_menu = SettingsMenu()
self.settings_menu.show()
class TrayIcon(PyQt5.QtWidgets.QSystemTrayIcon):
def __init__(self):
super().__init__()
self.setIcon(PyQt5.QtGui.QIcon('icon.xpm'))
self.setContextMenu(ContextMenu())
if __name__ == '__main__':
app = PyQt5.QtWidgets.QApplication(sys.argv)
tray_icon = TrayIcon()
tray_icon.show()
sys.exit(app.exec_())