pythonpython-3.xpyqt5qmainwindowqsystemtrayicon

PyQt5: how do I launch a window from a system tray icon context menu?


I have two separate files, one that creates a system tray icon and context menu, and another a window to take user input.

traywindow.py contains this:

import sys
import os
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QWidget, QLabel, QLineEdit
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtCore import QSize    

class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        self.setMinimumSize(QSize(320, 172))    
        self.setWindowTitle("Set Account") 

        # Setup username field
        self.username_label = QLabel(self)
        self.username_label.setText('Username')
        self.username_field = QLineEdit(self)
        self.username_label.move(45, 20)
        self.username_field.move(115, 23)
        self.username_field.resize(150, 25)

        # Setup OK button
        pybutton = QPushButton('OK', self)
        pybutton.clicked.connect(self.buttonClicked)
        pybutton.resize(100,32)
        pybutton.move(110, 128)        

    def buttonClicked(self):
        print('Username: ' + self.username_field.text())

def displayWindow():
    app = QtWidgets.QApplication(sys.argv)
    preferences_window = MainWindow()
    preferences_window.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    displayWindow()

Which produces:

enter image description here

trayapp.py contains this:

import os
import sys
import traywindow
from PyQt5 import QtWidgets, QtCore, QtGui

class SystemTrayIcon(QtWidgets.QSystemTrayIcon):

    def __init__(self, icon, parent=None):
        QtWidgets.QSystemTrayIcon.__init__(self, icon, parent)

        menu = QtWidgets.QMenu(parent)
        prefs_action = menu.addAction('Preferences...')
        self.setContextMenu(menu)
        prefs_action.triggered.connect(self.setPrefs)

    def setPrefs(self):
        traywindow.displayWindow()

def main(image):
    app = QtWidgets.QApplication(sys.argv)
    w = QtWidgets.QWidget()
    trayIcon = SystemTrayIcon(QtGui.QIcon(image), w)
    trayIcon.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    icon = 'icon.png'
    main(icon)

Which produces:

Window

I want to launch the window from my context menu, so when I click on Preferences... it will open the Set Account window, and when I fill in the field and click on OK, capture that into variables/pass those along as arguments in the trayapp.py file. Currently, my attempt in the code above gives me this when I click on Preferences...:

Traceback (most recent call last):
  File "/Users/Username/Downloads/trayapp.py", line 17, in setPrefs
    traywindow.displayWindow()
  File "/Users/Username/Downloads/traywindow.py", line 36, in displayWindow
    sys.exit(app.exec_())
SystemExit: -1
[Finished in 3.0s with exit code -6]

I'm new to PyQt5 and feel like I'm missing something fundamental. From the error I think it's to do with how each file is called at the end to produce its UI, but I haven't found an answer in the docs so far.


Solution

  • You can only have one QApplication and in displayWindow you are creating a second QApplication, instead create a MainWindow and do not call displayWindow.

    def setPrefs(self):
        self.window = traywindow.MainWindow()
        self.window.show()