popup window from tray icon that doesn't close the program when the window is closed

1.6k Views Asked by At

Setting up a toolkit that launches different applications from a tray Icon and I need to be able to open the configuration window and then close it without closing the entire application.

import sys
from PyQt5.QtWidgets import QSystemTrayIcon, QApplication, QMenu, qApp, QMainWindow, QPushButton, QLabel, QLineEdit
from PyQt5.QtGui import QIcon
from PyQt5 import QtCore

class autoparse():
    def __init__(self):
        self.main()

    def main(self):
        app = QApplication(sys.argv)
        self.trayIcon = QSystemTrayIcon(QIcon("icons\icon-windowed.ico"), app)
        self.menu = QMenu()
        self.trayIcon.setContextMenu(self.menu)
        self.autopconfig = self.menu.addAction('Config')
        self.autopconfig.triggered.connect(self.configwindow)
        exitaction = self.menu.addAction("Exit")
        exitaction.triggered.connect(qApp.quit)

        self.trayIcon.show()
        sys.exit(app.exec_())

    def configwindow(self):
        try:
            self.config = QMainWindow()
            self.config.setWindowTitle('Configuration')
            self.config.setGeometry(300, 300, 640, 480)

            self.lbl = QLabel('Directory: ', self.config)
            self.lbl.setGeometry(QtCore.QRect(10, 20, 200, 20))
            self.pathsel = QLineEdit(self.config)
            self.pathsel.setMaxLength(250)
            self.pathsel.setText('path here')
            # self.pathsel.selectAll()
            self.pathsel.setGeometry(QtCore.QRect(10, 50, 400, 20))
            print(self.pathsel.text())

            self.btn = QPushButton('...', self.config)
            self.btn.setGeometry(QtCore.QRect(414, 50, 30, 20))
            self.btn.clicked.connect(self.fileselect)

            self.config.show()
        except Exception as E:
            print(E)
    def fileselect(self):
        print('hello')

test1 = autoparse()

I assume its closing the entire application because my popup window is Qmainwindow() but the only other popup type windows im finding are dialog windows that are auto populated with fields. Maybe I need to launch mainwindow when the tray icon launches and then hide() the mainwindow? Then launch the popup windows with that as the parent?

End-goal: I want to select options from the tray icon and get windows that come up with my configured information. When someone clicks "okay", "save", "cancel", etc in one of these windows or clicks the X I do not want it to exit the application and remove the tray icon.

1

There are 1 best solutions below

0
On BEST ANSWER

If you do not want your app to close when you press x or close the sale you must use the setQuitOnLastWindowClosed(False).

def main(self):
    app = QApplication(sys.argv)
    QApplication.setQuitOnLastWindowClosed(False)
    self.trayIcon = QSystemTrayIcon(QIcon("icons\icon-windowed.ico"), app)