0

I've made a small program with PyQT5 and SysTrayIcon (infi.systray), and if the program is closed (person pressed "X"), there is an option to open it with TrayIcon.

enter image description here

If the button "Open" pressed (in TrayIcon"), then the program must show as it was at the beginning

enter image description here

however, it is starting but not responding

enter image description here

Why? And how to fix it?
My code:

import sys
from infi.systray import SysTrayIcon
from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(555,333)
        MainWindow.setMinimumSize(QtCore.QSize(555, 333))
        MainWindow.setMaximumSize(QtCore.QSize(555, 333))
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setMinimumSize(QtCore.QSize(555, 333))
        self.centralwidget.setMaximumSize(QtCore.QSize(555, 333))
        font = QtGui.QFont()
        font.setFamily("Comfortaa")
        font.setPointSize(15)
        self.centralwidget.setFont(font)
        self.centralwidget.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
        self.centralwidget.setStyleSheet("background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(150, 2, 255, 255), stop:1 rgba(255, 0, 230, 255));")
        self.centralwidget.setObjectName("centralwidget")
# Logo
        self.logo = QtWidgets.QLabel(self.centralwidget)
        self.logo.setGeometry(QtCore.QRect(50, 30, 201, 221))
        self.logo.setStyleSheet("background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(0, 0, 0, 0), stop:1 rgba(255, 255, 255, 0));")
        self.logo.setText("")
        self.logo.setPixmap(QtGui.QPixmap("/photos/logo.png"))
        self.logo.setObjectName("logo")
# Rules
        self.Rules = QtWidgets.QLabel(self.centralwidget)
        self.Rules.setGeometry(QtCore.QRect(300, 30, 300, 200))
        self.Rules.setStyleSheet("background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(0, 0, 0, 0), stop:1 rgba(255, 255, 255, 0));\n"
"font: 25 10pt \"Comfortaa\";")
        self.Rules.setWordWrap(True)
        self.Rules.setObjectName("Rules")
        MainWindow.setCentralWidget(self.centralwidget)


        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)



# Trayicon
        def open(systray):
            MainWindow.show()
        def info(systray):
            print("Info")
        def help(systray):
            print("Help")
        menu_options = (("Open", None, open), ("Info", None, info),("Help", None, help))
        systray = SysTrayIcon("/icons/logo.ico", "App", menu_options)
        systray.start()


    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Program"))
        self.Rules.setText(_translate("MainWindow", "Short rules how to use it:"))




if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    app_icon = QtGui.QIcon()
    app_icon.addFile('/icons/logo.ico')
    app.setWindowIcon(app_icon)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
  • 1
    I don't know what that systray actually does, but it's possible that it's blocking OR that it doesn't work well with the Qt main thread. Since Qt already provides such a feature with [QSystemTrayIcon](https://doc.qt.io/qt-5/qsystemtrayicon.html), I strongly suggest to use that instead of relying on other libraries. – musicamante Dec 09 '20 at 15:00
  • By default, QApplication quits when the last window is closed. To prevent this you need to set `app.setQuitOnLastWindowClosed(False)` – Heike Dec 09 '20 at 15:01
  • 1
    Also, you really should **not** edit the files generated with `pyuic`, as they are intended to be used as imported modules *only*. Read more about the correct ways to use them in the official guidelines about [using Designer](https://www.riverbankcomputing.com/static/Docs/PyQt5/designer.html). – musicamante Dec 09 '20 at 15:02
  • @musicamante I tried it, but I was unable to quit (exit) program, Also I don't know how to print text with PyQT tray icon –  Dec 09 '20 at 15:02
  • @Heike at the end? –  Dec 09 '20 at 15:04
  • @AlexZab Are you referring to the QSystemTrayIcon? If that's so, create a new question about it. The `setQuitOnLastWindowClosed(False)` must be set *before* calling `app.exec_()`. – musicamante Dec 09 '20 at 15:05
  • @musicamante No, I'm not referring to the QSystemTrayIcon. Thank You –  Dec 09 '20 at 15:06
  • @AlexZab ok, so what were you referring to? And what do you mean by "I don't know how to print text with PyQT tray icon"? – musicamante Dec 09 '20 at 15:07
  • @Heike, Thank You very much! –  Dec 09 '20 at 15:09

1 Answers1

0

You need to add app.setQuitOnLastWindowClosed(False) at the end, so your code will look like:

import sys
from infi.systray import SysTrayIcon
from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(555,333)
        MainWindow.setMinimumSize(QtCore.QSize(555, 333))
        MainWindow.setMaximumSize(QtCore.QSize(555, 333))
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setMinimumSize(QtCore.QSize(555, 333))
        self.centralwidget.setMaximumSize(QtCore.QSize(555, 333))
        font = QtGui.QFont()
        font.setFamily("Comfortaa")
        font.setPointSize(15)
        self.centralwidget.setFont(font)
        self.centralwidget.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
        self.centralwidget.setStyleSheet("background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(150, 2, 255, 255), stop:1 rgba(255, 0, 230, 255));")
        self.centralwidget.setObjectName("centralwidget")
# Logo
        self.Elsi_logo = QtWidgets.QLabel(self.centralwidget)
        self.Elsi_logo.setGeometry(QtCore.QRect(50, 30, 201, 221))
        self.Elsi_logo.setStyleSheet("background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(0, 0, 0, 0), stop:1 rgba(255, 255, 255, 0));")
        self.Elsi_logo.setText("")
        self.Elsi_logo.setPixmap(QtGui.QPixmap("/photos/logo.png"))
        self.Elsi_logo.setObjectName("logo")
# Rules
        self.Rules = QtWidgets.QLabel(self.centralwidget)
        self.Rules.setGeometry(QtCore.QRect(300, 30, 300, 200))
        self.Rules.setStyleSheet("background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(0, 0, 0, 0), stop:1 rgba(255, 255, 255, 0));\n"
"font: 25 10pt \"Comfortaa\";")
        self.Rules.setWordWrap(True)
        self.Rules.setObjectName("Rules")
        MainWindow.setCentralWidget(self.centralwidget)


        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)



# Trayicon
        def open(systray):
            MainWindow.show()
        def info(systray):
            print("Info")
        def help(systray):
            print("Help")
        menu_options = (("Open", None, open), ("Info", None, info),("Help", None, help))
        systray = SysTrayIcon("/icons/logo.ico", "App", menu_options)
        systray.start()


    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Program"))
        self.Rules.setText(_translate("MainWindow", "Short rules how to use it:"))




if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    app_icon = QtGui.QIcon()
    app_icon.addFile('/icons/logo.ico')
    app.setWindowIcon(app_icon)
    app.setQuitOnLastWindowClosed(False)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())