# -*- coding: utf-8 -*-
# python:2.x
__author__ = 'Administrator'
from PyQt4.QtGui import *
from PyQt4.Qt import *
from PyQt4.QtCore import *
from tp import Ui_Form
import sys
from PyQt4.QtCore import Qt
QTextCodec.setCodecForTr(QTextCodec.codecForName("utf8"))
class Example(QDialog,Ui_Form):
def __init__(self,parent=None):
super(Example, self).__init__(parent)
self.setupUi(self)
self.setContextMenuPolicy(Qt.CustomContextMenu)
self.connect(self,SIGNAL('customContextMenuRequested(const QPoint&)'),self.on_context)
self.createContexMenu()
self.tp.clicked.connect(self.bsfs)
def bsfs(self):
self.setContextMenuPolicy(Qt.CustomContextMenu)
self.x1=QMenu(self)
self.a1=self.x1.addAction(u'托盘')
self.a1.triggered.connect(self.menus)
self.x1.exec_(self.mapToGlobal(QPoint(130,98)))
def on_context(self,point):
self.contextMenu.exec_(self.mapToGlobal(point))
def createContexMenu(self):
"""
创建右击菜单
"""
self.setContextMenuPolicy(Qt.CustomContextMenu)
self.contextMenu=QMenu(self)
self.bsfsc=self.contextMenu.addAction(u'托盘')
self.trayicon=QSystemTrayIcon(self)
self.trayicon.setIcon(QIcon(r'tp.png'))
self.trayicon.setToolTip(u'托盘')
self.bsfsc.triggered.connect(self.menus)
def showMessage(self):
icon=QSystemTrayIcon.Information
self.trayicon.showMessage(u"提示信息",u"点我干嘛?",icon)
def trayclick(self,res):
if res==QSystemTrayIcon.DoubleClick:
self.showNormal()
elif res==QSystemTrayIcon.MiddleClick:#中击
self.showMessage()
def menus(self):
self.min=QAction(u'最小化',self,triggered=self.hide)
self.max=QAction(u'最大化',self,triggered=self.showMaximized)
self.Hy=QAction(u'还愿',self,triggered=self.showNormal)
self.qiuct=QAction(u'退出',self,triggered=qApp.quit)
self.traymen=QMenu(QApplication.desktop())
self.traymen.addAction(self.min)
self.traymen.addAction(self.max)
self.traymen.addAction(self.Hy)
self.traymen.addAction(self.qiuct)
self.trayicon.setContextMenu(self.traymen)
self.trayicon.activated.connect(self.trayclick)
self.trayicon.show()
self.qiuct.triggered.connect(self.qiut)
def qiut(self):
self.showMessage()
self.close()
def activation(self,activation_reason):#托盘
if activation_reason==QSystemTrayIcon.Trigger:
self.show()
app =QApplication(sys.argv)
QApplication.setQuitOnLastWindowClosed(False)
x = Example()
x.show()
sys.exit(app.exec_())
如图: