为什么我的程序在我想显示后没有响应?
Why my program is not responding after I want to show it?
我用 PyQT5 和 SysTrayIcon (infi.systray) 制作了一个小程序,如果程序关闭(用户按下“X”),可以选择用 TrayIcon 打开它。
如果按下“打开”按钮(在 TrayIcon 中),则程序必须显示为开始时的样子
然而,正在启动但没有响应
为什么?以及如何修复它?
我的代码:
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_())
您需要在末尾添加 app.setQuitOnLastWindowClosed(False)
,因此您的代码将如下所示:
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_())
我用 PyQT5 和 SysTrayIcon (infi.systray) 制作了一个小程序,如果程序关闭(用户按下“X”),可以选择用 TrayIcon 打开它。
如果按下“打开”按钮(在 TrayIcon 中),则程序必须显示为开始时的样子
然而,正在启动但没有响应
为什么?以及如何修复它?
我的代码:
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_())
您需要在末尾添加 app.setQuitOnLastWindowClosed(False)
,因此您的代码将如下所示:
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_())