#UI.py,通过UI设计师制作后直接转换为UI.py脚本
# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(400, 300)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8))
#UI.py,可视化UI.py
# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui, Qt
from UI import *
class MainWindow(QtGui.QMainWindow):
def __init__(self,parent=None):
QtGui.QWidget.__init__(self,parent)
self.ui=Ui_Form()
self.ui.setupUi(self)
#显示托盘图标
self.trayIcon = QtGui.QSystemTrayIcon(self)
self.trayIcon.setIcon(QtGui.QIcon("images/logo.png"))
self.trayIcon.show()
#双击托盘信号槽
self.trayIcon.activated.connect(self.trayClick)
#激活右键托盘菜单
self.trayMenu()
#定义双击托盘事件
def trayClick(self,reason):
if reason==QtGui.QSystemTrayIcon.DoubleClick:
self.showNormal()
else:
pass
#创建右击托盘菜单
def trayMenu(self):
#托盘提示信息
self.trayIcon.setToolTip("托盘测试软件")
#菜单项1
img_open = QtGui.QIcon("images\logo.png")
self.restoreAction = QtGui.QAction(img_open,"打开主窗口", self)
self.restoreAction.triggered.connect(self.showNormal)
#菜单项2
self.quitAction = QtGui.QAction("退出", self)
self.quitAction.triggered.connect(QtGui.qApp.quit)
#创建托盘目录
self.trayIconMenu = QtGui.QMenu(self)
self.trayIconMenu.addAction(self.restoreAction)
self.trayIconMenu.addSeparator()#添加分隔线
self.trayIconMenu.addAction(self.quitAction)
#设置目录为创建的目录
self.trayIcon.setContextMenu(self.trayIconMenu)
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
myapp=MainWindow()
myapp.show()
app.exec_()