2

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_())
Community
  • 1
  • 1
s_gintou
  • 51
  • 6

1 Answers1

3
app.setQuitOnLastWindowClosed(False)

solved my problem. It seems that PyQt5 counts only opened and displayed windows, and TrayIcon does not count as one of them.

s_gintou
  • 51
  • 6
  • This solved my problem. Closing any new window, including selecting a file in QFileDialog, was quickly making my app exit (exit normally I must add). Your answer solved my problem, thanks a million! – Paddy Harrison Jul 26 '20 at 18:28