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 也运行不了,目前未找到原因!

Pyqt QSystemTrayIcon 实现托盘效果的更多相关文章

  1. 在C#实现托盘效果(转)

    桌面程序的开发中,经常考虑能在状态栏实现托盘快捷操作,托盘程序的实现在API时代,还是相对复杂的,首先在MSDN中可以查看其函数细节, 然后在根据其要求的参数进行复杂的设置.      在.NET时代 ...

  2. .Net程序随系统开机启动(仿Foxmail托盘效果控制)

    对于使.NET程序随系统开机启动,最常用的可能就是向在注册表中注册开机启动项,或是建立Windows服务,使程序随系统启动而启动.这里以WinForm程序为例,测试Demo分享,同时附上对于程序托盘的 ...

  3. Qt5 托盘模仿qq闪烁,弹消息框实现

    在别人代码基础上做的,课设刚好用上了,贴出来分享Qt5.5.1实现. 图片自己找. #ifndef DIALOG_H #define DIALOG_H #include <QDialog> ...

  4. Qt5 任务栏托盘功能实现

    23333 有一阵子没写博客了,研究了挺长时间qt,学到任务栏托盘时简直无语,网上找得到的代码大多是废码,Qt5不支持或者本身就有毛病不能实现却被n多人转来转去的,甚是无语. 简单托盘功能以下在Qt5 ...

  5. Qt托盘程序

    使用QSystemTrayIcon类可以实现托盘程序.在这里使用QMainWindow做实例: mainwindow.h头文件 #ifndef MAINWINDOW_H #define MAINWIN ...

  6. QT 托盘 hover事件捕捉

    1. QSystemTrayIcon hover事件 参考:https://stackoverflow.com/questions/21795919/how-to-catch-the-mousehov ...

  7. Qt浅谈之总结(整理)

    Qt浅谈之总结(整理) 来源 http://blog.csdn.net/taiyang1987912/article/details/32713781 一.简介 QT的一些知识点总结,方便以后查阅. ...

  8. 用swing也可以做出好看的界面

    用Swing做出的例子:JavaFX做出的界面:后来又做出了自己编写的一套基于Synth的L&F,其与直接在代码中重绘某个组件不同,最大优点是具有可插拔性,即在不改变原有程序代码的情况下,用户 ...

  9. 16Aspx.com源码2013年10月到2013年12月详细

    创建时间FROM: 创建时间TO:   ExtJS合同管理信息系统源码 2013-12-13   [VS2008] 源码介绍: ExtJS合同管理信息系统源码浏览器兼容:IE,Firefox,谷歌等主 ...

随机推荐

  1. Unity3D获取Andorid设备返回键,主页键等功能

    在Unity开发中捕捉Android的常用事件其实很简单 在新建的脚本文件中就加入: 比如: // 返回键 if ( Application.platform == RuntimePlatform.A ...

  2. zabbix之Nginx安装

    转载自 http://www.ttlsa.com/nginx/nginx-install-on-linux/ Nginx下载 https://pan.baidu.com/s/1qXT54sO

  3. CTSC2016游记

    打了几天酱油.. day1 3分滚..考场上打了5+0+3,5文件名挂了. (因为5那题我会nlog^3n做法,然而只是暴力分而已.(被KDTree艹过去的一题)) 提答xjb玩了三分,原因是exgc ...

  4. pycharm 中 pep8 检查开启.

    pycharm pep8检查的开启,默认是暗黄色,我这里为了醒目给改成了黄色.

  5. 【Unity3D】Invoke,InvokeRepeating ,Coroutine 延迟调用,周期性调用

    Invoke和InvokeRepeating方法,可以实现延迟调用,和周期调用 第一个是执行一次,第二个是重复执行 void Invoke(string methodName, float time) ...

  6. POJ 1062 ( dijkstra )

    http://poj.org/problem?id=1062 一个中文题,一个多月之前我做过,当时我是用搜索写的,不过苦于卡在无法确定等级关系,所以就错了. 看了别人的博客后,我还是不是很理解所谓的枚 ...

  7. 关于fill_parent,match_parent和wrap_content (转载)

    fill_parent&match_parent: 在Android2.2及以上版本中,fill_parent与match_parent意思相同(其中fill_parent兼容低版本).都是尽 ...

  8. ios delegate 使用注意 assign,weak

    今天一个同事写代码,把一个delegate对象设定成了assign类型属性,没有用weak,就是delegate对象释放后,不会把delegate指针自动设定为nil,把对象的delegate设定成了 ...

  9. DNS基础及域名系统架构

    本文出自   http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...

  10. Win7下同时使用有线和无线时的优先级设置

    终于找到这个问题的解决方案了!!!!我是通过方法1改跃点数实现的,方法2无效. http://linshengling.blog.163.com/blog/static/114651912012102 ...