Why my program is not responding after I want to show it?

101 Views Asked by At

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

There are 1 best solutions below

0
On BEST ANSWER

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_())