3

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.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
sidnical
  • 421
  • 1
  • 6
  • 23
  • What are the "okay", "save", "cancel", I do not see them in your application. – eyllanesc Jul 09 '17 at 23:53
  • @eyllanesc I have not added them yet but this brings up a good question. If the user clicks "okay" and that button runs a function that saves whatever configuration was in the window, how would I also close the configuration window at the end of that function? – sidnical Jul 10 '17 at 03:19
  • What type of window will you use, if you are going to use `QDialog` I recommend using the `exec_()` function instead of using show (), this will return the state, and you can associate the buttons with the task of closing the window, I recommend reading about `QDialog`. – eyllanesc Jul 10 '17 at 03:23
  • If you are creating forms use QDialog, QMainWindow has other goals. – eyllanesc Jul 10 '17 at 03:24
  • This points me in the right direction. Thanks – sidnical Jul 10 '17 at 03:25

1 Answers1

6

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)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241