PyQt 未在 XFCE / Ubuntu 16.04 Xenial Xerus 上显示 QSystemTrayIcon


#!/usr/bin/python
#
#  GUI.py
#  
#  Copyright 2015 Ognjen Galic <gala@thinkpad>
#  
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#  
#  
 ################################# UI START ############################
from PyQt4 import QtCore, QtGui
import sys
try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s
try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(200, 100)
        MainWindow.setWindowTitle(_fromUtf8("Example"))
        self.main_widget = QtGui.QWidget(MainWindow)
        self.main_widget.setObjectName(_fromUtf8("main_widget"))
        self.horizontalLayout = QtGui.QHBoxLayout(self.main_widget)
        self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
        self.exit_button = QtGui.QPushButton(self.main_widget)
        self.exit_button.setObjectName(_fromUtf8("exit_button"))
        self.horizontalLayout.addWidget(self.exit_button)
        self.trayIcon = QtGui.QSystemTrayIcon(QtGui.QIcon("icon.png")) # <<<<<<<<<<< WONT SHOW
        MainWindow.setCentralWidget(self.main_widget)
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
        # --------------------------- QT ACTION BIND START #############
        self.exit_button.clicked.connect(self.exit_main)
        # --------------------------- QT ACTION BIND STOP ##############
    def retranslateUi(self, MainWindow):
        self.exit_button.setText(_translate("MainWindow", "Exit", None))
        # --------------------------- MAIN FUNCTIONS START #############
    def exit_main(self):
        sys.exit()
        # --------------------------- MAIN FUNCTIONS STOP ##############
def initUI():
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
 ################################### UI STOP ###########################
def main():
    pass
initUI() # Initialize the UI
main() # Run the MAIN

这是我的来源,Python 是 2.7,amd64,Ubuntu 16.04 Xenial Xerus 与 Xfce 4.12,PyQt4

应该显示系统托盘图标的类似内容:

self.trayIcon = QtGui.QSystemTrayIcon(QtGui.QIcon("icon.png")) 

什么都不做。然而,如果使用手动trayIcon.show()在另一个.py文件上运行,它可以正常工作。

您错过了节目部分:)

self.exit_button.clicked.connect(self.exit_main)
# --------------------------- QT ACTION BIND STOP ##############
self.trayIcon.show()

这对我有用

最新更新