我的qsystemtray有问题。当我运行代码时,这个代码没有"while",我可以看到图标。当我不能观看图标时(我检查程序是否进入条件"if(a.inbox.uread_count(>0"(,但图标一定不可用的区域。也就是说,托盘图标会出现,但不会绘制。有什么方法可以解决这个问题吗?操作系统:Astra-linux(fork-debian(
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'qttray.ui'
#
# Created by: PyQt5 UI code generator 5.10.1
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
import extest4
import time
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1014, 680)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setMaximumSize(QtCore.QSize(800, 547))
self.centralwidget.setObjectName("centralwidget")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.centralwidget)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.horizontalLayout_2.addLayout(self.horizontalLayout)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1014, 30))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
email = 'testemail'
server = '********'
tr = QtWidgets.QSystemTrayIcon(QtGui.QIcon(r'payments.PNG'), app)
print(tr.isSystemTrayAvailable())
while(1):
a = extest4.connect(server, email)
if(a.inbox.unread_count)>0:
a.inbox.refresh()
tr.setVisible(True)
tr.show()
else:
tr.hide()
extest4.close_connections()
time.sleep(10)
sys.exit(app.exec_())
您有三个阻塞条件:while
周期、邮件检查和time.sleep。所有这些条件都会以某种方式锁定接口(允许在应用程序主事件循环中绘制和交互(,其中while
是"最锁定"的一个:直到退出,GUI都会被阻塞,因此图标没有绘制(事实上,与GUI相关的任何东西都无法工作,包括主窗口(如果您已经显示了它(。
由于邮件检查可能需要一些时间来处理,您需要将其放在一个单独的线程中。
在这个例子中,我将逻辑放在主窗口的一个子类中,因为您可能会将其用作程序的主"容器"。其他替代方案包括使用QObject的子类或QApplication本身。
server = 'mail.mailserver.com'
email = 'myname@mailserver.com'
class MailChecker(QtCore.QThread):
mailAvailable = QtCore.pyqtSignal(bool)
def __init__(self, server, mail):
super().__init__()
self.server = server
self.mail = mail
def run(self):
while True:
a = extest4.connect(self.server, self.email)
if a.inbox.unread_count > 0:
a.inbox.refresh()
self.mailAvailable.emit(True)
else:
self.mailAvailable.emit(False)
extest4.close_connections()
QtCore.QThread.sleep(10)
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.trayIcon = QtWidgets.QSystemTrayIcon(
QtGui.QIcon(r'payments.PNG'), self)
self.trayIcon.activated.connect(self.show)
self.mailChecker = MailChecker(server, email)
self.mailChecker.mailAvailable.connect(self.trayIcon.setVisible)
self.mailChecker.start()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
mainWindow = MainWindow()
sys.exit(app.exec_())