pyqt的托盘效果很好实现,在Pyqt的demo中有个例子

路径:PyQt4\examples\desktop\systray.py

今天我就仿这个Tray效果做效果

一. 创建UI

trayicon.ui文件:

 <?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>TrayIcon</class>
<widget class="QWidget" name="TrayIcon">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>418</width>
<height>441</height>
</rect>
</property>
<property name="maximumSize">
<size>
<width>1024</width>
<height>16777215</height>
</size>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QGroupBox" name="groupBoxTrayIcon">
<property name="minimumSize">
<size>
<width>400</width>
<height>100</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>100</height>
</size>
</property>
<property name="title">
<string>托盘图标</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QHBoxLayout" name="horizontalLayoutByrayicon">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>图标: </string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="comboBoxIcon"/>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QCheckBox" name="checkBoxShowIcon">
<property name="text">
<string>展示图标</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QCheckBox" name="checkBoxQQmsg">
<property name="text">
<string>QQ消息效果</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBoxMessages">
<property name="enabled">
<bool>true</bool>
</property>
<property name="title">
<string>气泡消息</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="2" column="1">
<widget class="QLabel" name="label_5">
<property name="text">
<string>标题: </string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLabel" name="label_3">
<property name="text">
<string>持续时长: </string>
</property>
</widget>
</item>
<item row="3" column="2">
<widget class="QTextEdit" name="textEditContent"/>
</item>
<item row="0" column="1">
<widget class="QLabel" name="label_2">
<property name="text">
<string>类型: </string>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QComboBox" name="comboBox_MsgInfo">
<property name="maximumSize">
<size>
<width>100</width>
<height>16777215</height>
</size>
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QSpinBox" name="spinBoxTime">
<property name="enabled">
<bool>true</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Fixed">
<horstretch>60</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>60</width>
<height>16777215</height>
</size>
</property>
</widget>
</item>
<item row="2" column="2">
<widget class="QLineEdit" name="lineEditTitle"/>
</item>
<item row="3" column="1">
<widget class="QLabel" name="label_6">
<property name="text">
<string>内容: </string>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop</set>
</property>
</widget>
</item>
<item row="4" column="2">
<widget class="QPushButton" name="ShowButton">
<property name="maximumSize">
<size>
<width>75</width>
<height>16777215</height>
</size>
</property>
<property name="text">
<string>展示消息</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

uic转换为py

trayicon.py文件:

 # -*- coding: utf-8 -*-

 # Form implementation generated from reading ui file 'trayicon.ui'
#
# Created: Tue Mar 03 17:34:43 2015
# by: PyQt4 UI code generator 4.10.3
#
# WARNING! All changes made in this file will be lost! from PyQt4 import QtCore, QtGui 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_TrayIcon(object):
def setupUi(self, TrayIcon):
TrayIcon.setObjectName(_fromUtf8("TrayIcon"))
TrayIcon.resize(418, 441)
TrayIcon.setMaximumSize(QtCore.QSize(1024, 16777215))
self.verticalLayout = QtGui.QVBoxLayout(TrayIcon)
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.groupBoxTrayIcon = QtGui.QGroupBox(TrayIcon)
self.groupBoxTrayIcon.setMinimumSize(QtCore.QSize(400, 100))
self.groupBoxTrayIcon.setMaximumSize(QtCore.QSize(16777215, 100))
self.groupBoxTrayIcon.setObjectName(_fromUtf8("groupBoxTrayIcon"))
self.verticalLayout_2 = QtGui.QVBoxLayout(self.groupBoxTrayIcon)
self.verticalLayout_2.setObjectName(_fromUtf8("verticalLayout_2"))
self.horizontalLayoutByrayicon = QtGui.QHBoxLayout()
self.horizontalLayoutByrayicon.setObjectName(_fromUtf8("horizontalLayoutByrayicon"))
self.label = QtGui.QLabel(self.groupBoxTrayIcon)
self.label.setObjectName(_fromUtf8("label"))
self.horizontalLayoutByrayicon.addWidget(self.label)
self.comboBoxIcon = QtGui.QComboBox(self.groupBoxTrayIcon)
self.comboBoxIcon.setObjectName(_fromUtf8("comboBoxIcon"))
self.horizontalLayoutByrayicon.addWidget(self.comboBoxIcon)
spacerItem = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
self.horizontalLayoutByrayicon.addItem(spacerItem)
self.checkBoxShowIcon = QtGui.QCheckBox(self.groupBoxTrayIcon)
self.checkBoxShowIcon.setObjectName(_fromUtf8("checkBoxShowIcon"))
self.horizontalLayoutByrayicon.addWidget(self.checkBoxShowIcon)
self.verticalLayout_2.addLayout(self.horizontalLayoutByrayicon)
self.checkBoxQQmsg = QtGui.QCheckBox(self.groupBoxTrayIcon)
self.checkBoxQQmsg.setObjectName(_fromUtf8("checkBoxQQmsg"))
self.verticalLayout_2.addWidget(self.checkBoxQQmsg)
self.verticalLayout.addWidget(self.groupBoxTrayIcon)
self.groupBoxMessages = QtGui.QGroupBox(TrayIcon)
self.groupBoxMessages.setEnabled(True)
self.groupBoxMessages.setObjectName(_fromUtf8("groupBoxMessages"))
self.gridLayout = QtGui.QGridLayout(self.groupBoxMessages)
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.label_5 = QtGui.QLabel(self.groupBoxMessages)
self.label_5.setObjectName(_fromUtf8("label_5"))
self.gridLayout.addWidget(self.label_5, 2, 1, 1, 1)
self.label_3 = QtGui.QLabel(self.groupBoxMessages)
self.label_3.setObjectName(_fromUtf8("label_3"))
self.gridLayout.addWidget(self.label_3, 1, 1, 1, 1)
self.textEditContent = QtGui.QTextEdit(self.groupBoxMessages)
self.textEditContent.setObjectName(_fromUtf8("textEditContent"))
self.gridLayout.addWidget(self.textEditContent, 3, 2, 1, 1)
self.label_2 = QtGui.QLabel(self.groupBoxMessages)
self.label_2.setObjectName(_fromUtf8("label_2"))
self.gridLayout.addWidget(self.label_2, 0, 1, 1, 1)
self.comboBox_MsgInfo = QtGui.QComboBox(self.groupBoxMessages)
self.comboBox_MsgInfo.setMaximumSize(QtCore.QSize(100, 16777215))
self.comboBox_MsgInfo.setObjectName(_fromUtf8("comboBox_MsgInfo"))
self.gridLayout.addWidget(self.comboBox_MsgInfo, 0, 2, 1, 1)
self.spinBoxTime = QtGui.QSpinBox(self.groupBoxMessages)
self.spinBoxTime.setEnabled(True)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(60)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.spinBoxTime.sizePolicy().hasHeightForWidth())
self.spinBoxTime.setSizePolicy(sizePolicy)
self.spinBoxTime.setMaximumSize(QtCore.QSize(60, 16777215))
self.spinBoxTime.setObjectName(_fromUtf8("spinBoxTime"))
self.gridLayout.addWidget(self.spinBoxTime, 1, 2, 1, 1)
self.lineEditTitle = QtGui.QLineEdit(self.groupBoxMessages)
self.lineEditTitle.setObjectName(_fromUtf8("lineEditTitle"))
self.gridLayout.addWidget(self.lineEditTitle, 2, 2, 1, 1)
self.label_6 = QtGui.QLabel(self.groupBoxMessages)
self.label_6.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignTop)
self.label_6.setObjectName(_fromUtf8("label_6"))
self.gridLayout.addWidget(self.label_6, 3, 1, 1, 1)
self.ShowButton = QtGui.QPushButton(self.groupBoxMessages)
self.ShowButton.setMaximumSize(QtCore.QSize(75, 16777215))
self.ShowButton.setObjectName(_fromUtf8("ShowButton"))
self.gridLayout.addWidget(self.ShowButton, 4, 2, 1, 1)
self.verticalLayout.addWidget(self.groupBoxMessages) self.retranslateUi(TrayIcon)
QtCore.QMetaObject.connectSlotsByName(TrayIcon) def retranslateUi(self, TrayIcon):
TrayIcon.setWindowTitle(_translate("TrayIcon", "Form", None))
self.groupBoxTrayIcon.setTitle(_translate("TrayIcon", "托盘图标", None))
self.label.setText(_translate("TrayIcon", "图标: ", None))
self.checkBoxShowIcon.setText(_translate("TrayIcon", "展示图标", None))
self.checkBoxQQmsg.setText(_translate("TrayIcon", "QQ消息效果", None))
self.groupBoxMessages.setTitle(_translate("TrayIcon", "气泡消息", None))
self.label_5.setText(_translate("TrayIcon", "标题: ", None))
self.label_3.setText(_translate("TrayIcon", "持续时长: ", None))
self.label_2.setText(_translate("TrayIcon", "类型: ", None))
self.label_6.setText(_translate("TrayIcon", "内容: ", None))
self.ShowButton.setText(_translate("TrayIcon", "展示消息", None)) if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
TrayIcon = QtGui.QWidget()
ui = Ui_TrayIcon()
ui.setupUi(TrayIcon)
TrayIcon.show()
sys.exit(app.exec_())

二.逻辑的实现

新建mainTray.py文件,内容:

 # -*- coding: UTF8 -*-
# UI说明: 新建窗体,添加两个groupbox 右键 -- 布局 -- 垂直布局 import sip
sip.setapi('QVariant', 2)
from PyQt4 import QtCore, QtGui
from trayicon import Ui_TrayIcon
import threading
import icoqrc class mainTray(QtGui.QWidget):
def __init__(self):
super(mainTray, self).__init__()
self.Ui= Ui_TrayIcon()
self.Ui.setupUi(self)
self.setWindowTitle(u'Pyqt 托盘效果')
self.setWindowIcon(QtGui.QIcon(':chrome.ico'))
# 填充Ui内容
self.supplyUi() # 创建icon
self.createTrayIcon() #通知区域icon显示
self.Ui.comboBoxIcon.currentIndexChanged.connect(self.setIcon) # 链接信号槽
self.Ui.comboBoxIcon.setCurrentIndex(1) # 设置当前combox
self.trayIcon.activated.connect(self.iconActivated) # 触发托盘事件
self.Ui.checkBoxShowIcon.toggled.connect(self.trayIcon.setVisible) # 触发是否显示托盘图标
self.trayIcon.show() # 托盘show
self.Ui.ShowButton.clicked.connect(self.showMessage) # 触发展示消息
self.trayIcon.messageClicked.connect(self.messageClicked) # 点击提示消息
self.threadTask() # 线程任务
self.Ui.checkBoxQQmsg.toggled.connect(self.QQmsg) # 触发QQ消息效果 def supplyUi(self):
# 托盘图标
self.Ui.comboBoxIcon.addItem(QtGui.QIcon(':chrome.ico'), u'Chrome')
self.Ui.comboBoxIcon.addItem(QtGui.QIcon(':firefox.ico'), u'Firefox')
self.Ui.comboBoxIcon.addItem(QtGui.QIcon(':qq.ico'), u'QQ')
self.Ui.comboBoxIcon.addItem(QtGui.QIcon(':flash.ico'), u'Flash')
self.Ui.comboBoxIcon.addItem(QtGui.QIcon(':ie.ico'), u'IE')
self.Ui.comboBoxIcon.addItem(QtGui.QIcon(':myfavicon.ico'), u'Favicon') # 默认展示托盘图标
self.Ui.checkBoxShowIcon.setChecked(True) # 消息combox
self.Ui.comboBox_MsgInfo.addItem("None", QtGui.QSystemTrayIcon.NoIcon)
self.Ui.comboBox_MsgInfo.addItem(self.style().standardIcon(QtGui.QStyle.SP_MessageBoxInformation), u"信息提示", QtGui.QSystemTrayIcon.Information)
self.Ui.comboBox_MsgInfo.addItem(self.style().standardIcon(QtGui.QStyle.SP_MessageBoxWarning), u"警告提示", QtGui.QSystemTrayIcon.Warning)
self.Ui.comboBox_MsgInfo.addItem(self.style().standardIcon(QtGui.QStyle.SP_MessageBoxCritical), u"严重警告", QtGui.QSystemTrayIcon.Critical) # 时长显示
self.Ui.spinBoxTime.setRange(5, 60) # spinbox 在5--60 之间
self.Ui.spinBoxTime.setSuffix(" s") # 设置后缀 s 秒
self.Ui.spinBoxTime.setValue(15) # 默认值为15秒 # 设置 标题和提示的初始化内容
self.Ui.lineEditTitle.setText(u'无法连接到网络')
self.Ui.textEditContent.setText(u'您的电脑无法连接到网络,请确保已经接入Internet,or WLAN 端口已经插好! 有问题请致电:<b>1389876543</b>')
# 创建icon 与菜单
def createTrayIcon(self):
self.minimizeAction = QtGui.QAction(u"最小化", self, triggered=self.hide)
self.maximizeAction = QtGui.QAction(u"最大化", self, triggered=self.showMaximized)
self.restoreAction = QtGui.QAction(u"还原大小", self, triggered=self.showNormal)
self.quitAction = QtGui.QAction(u"退出", self, triggered=QtGui.qApp.quit)
self.trayIconMenu = QtGui.QMenu(self)
self.trayIconMenu.addAction(self.minimizeAction)
self.trayIconMenu.addAction(self.maximizeAction)
self.trayIconMenu.addAction(self.restoreAction)
self.trayIconMenu.addSeparator() # 分割行
self.trayIconMenu.addAction(self.quitAction)
self.trayIcon = QtGui.QSystemTrayIcon(self)
self.trayIcon.setContextMenu(self.trayIconMenu)
# 触发托盘icon
def iconActivated(self, reason):
if reason in (QtGui.QSystemTrayIcon.Trigger, QtGui.QSystemTrayIcon.DoubleClick):
self.Ui.comboBoxIcon.setCurrentIndex((self.Ui.comboBoxIcon.currentIndex() + 1) % self.Ui.comboBoxIcon.count())
elif reason == QtGui.QSystemTrayIcon.MiddleClick: # 点击鼠标滚动轴事件
self.showMessage()
# 设置icon
def setIcon(self, index):
icon = self.Ui.comboBoxIcon.itemIcon(index)
self.trayIcon.setIcon(icon)
self.setWindowIcon(icon)
self.trayIcon.setToolTip(self.Ui.comboBoxIcon.itemText(index)) # 展示消息
def showMessage(self):
icon = QtGui.QSystemTrayIcon.MessageIcon(self.Ui.comboBox_MsgInfo.itemData(self.Ui.comboBox_MsgInfo.currentIndex()))
self.trayIcon.showMessage(self.Ui.lineEditTitle.text(), self.Ui.textEditContent.toPlainText(), icon, self.Ui.spinBoxTime.value() * 1000) # 点击消息
def messageClicked(self):
QtGui.QMessageBox.information(None, "Systray","Sorry, I already gave what help I could.\nMaybe you should " "try asking a human?") # 添加一个线程
def threadTask(self):
global t
t = threading.Timer(6.0, self.showMessage) # 6秒后执行显示消息
t.start() def QQmsg(self):
import time
status = self.Ui.checkBoxQQmsg.isChecked()
comboxicoIndex = self.Ui.comboBoxIcon.currentIndex()
icon = self.Ui.comboBoxIcon.itemIcon(comboxicoIndex)
arrs = 1
whiles = 1
if status:
while whiles <= 6:
if arrs == 1:
time.sleep(0.6)
self.trayIcon.setIcon(QtGui.QIcon())
arrs = 2
else:
time.sleep(0.6)
self.trayIcon.setIcon(icon)
arrs = 1
whiles += 1 else:
self.trayIcon.setIcon(icon) def keyPressEvent(self, event):
if event.key() ==QtCore.Qt.Key_Escape:
self.hide() if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
QtGui.QApplication.setQuitOnLastWindowClosed(False)
trany = mainTray()
trany.show()
sys.exit(app.exec_())

三.效果

四.打包出现问题

使用pyinstaller 打包exe,运行不了,即使打包Pyqt4里面的demo 也运行不了,目前未找到原因!

最新文章

  1. 20145215&amp;20145307信息安全系统设计基础实验报告
  2. Node.js学习-1
  3. CentOS6.x安装配置nginx
  4. No configuration found for the specified action解决办法
  5. Oracle实例和服务知识点
  6. 1205. By the Underground or by Foot?(spfa)
  7. 盒模型------CSS
  8. uva 11019 Matrix Matcher
  9. 响应式布局之BootStrap
  10. android-意图Intent
  11. MAP--复杂map结构的构造
  12. FreeMarker生成word的代码
  13. 安卓笔记--Edittext禁止换行
  14. 利用win10自带的虚拟机Hyper-V安装Centos7的步骤教程
  15. visual studio 版本管理从tfs迁移到svn
  16. 【巷子】---webpack配置非CMD规范的模块
  17. Socket编程 - 网络基础知识
  18. 使用JSON语法创建JS对象(重要)
  19. Centos7.5静默安装Oracle18c
  20. pandas如何统计所有列的空值,并转化为list?

热门文章

  1. 对C++虚函数、虚函数表的简单理解
  2. java定时器的几种用法[转]
  3. 试用vSphere 6(三):安装vCenter 6(独立数据库)之:vCenter安装与配置
  4. centos7 安装mysql5.7.11注意事项
  5. FFmpeg frei0r water 滤镜
  6. ubuntu apc 安装
  7. javax.transaction.xa.XAException: java.sql.SQLException: 无法创建 XA 控制连接。错误: 未能找到存储过程 &#39;master..xp_sqljdbc_xa_init&#39;
  8. windows下ftp命令大全
  9. linux权限不够,sh不能用
  10. mybatis参数错误 Parameter &#39;&#215;&#215;&#215;&#39; not found. Available parameters are [0, 1, param1, param2]