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. (原创)android中使用相机的两种方式

    在社交类应用或扫描二维码的场合都需要用到手机上的摄像头 在程序中启用这一硬件主要有两类方法 1.发送intent启动系统自带的摄像应用 此应用的AndroidManifest中的intent-filt ...

  2. java抓取快递100信息接口

    package zeze; import java.io.IOException; import org.json.JSONArray; import org.json.JSONException; ...

  3. Linux内核 TCP/IP、Socket参数调优

    Linux内核 TCP/IP.Socket参数调优 2014-06-06  Harrison....   阅 9611  转 165 转藏到我的图书馆   微信分享:   Doc1: /proc/sy ...

  4. 同级兄弟元素之间的CSS控制

    为了实现这个导航效果:选中菜单中某一项,隐藏掉自身的左边背景线条,同时让他的下一个兄弟元素也隐藏掉线条. 有一种选择器就叫兄弟元素选择器,分为临近兄弟和普通兄弟. 临近兄弟:用 + 表示,只匹配该元素 ...

  5. Expression Add Operators

    Given a string that contains only digits 0-9 and a target value, return all possibilities to add bin ...

  6. 虚函数列表: 取出方法 // 虚函数工作原理和(虚)继承类的内存占用大小计算 32位机器上 sizeof(void *) // 4byte

    #include <iostream> using namespace std; class A { public: A(){} virtual void geta(){ cout < ...

  7. Struts2获取Session方法

    Struts2里面有三种方法可以获取request,最好使用ServletRequestAware接口通过IOC机制注入Request对象. 方法1:IOC方式action类实现SessionAwar ...

  8. 使用logrotate管理nginx日志文件

    本文转载自:http://linux008.blog.51cto.com/2837805/555829 描述:linux日志文件如果不定期清理,会填满整个磁盘.这样会很危险,因此日志管理是系统管理员日 ...

  9. VIM常用操作总结

    VIM常用的模式网上说法不太一样,但是大同小异,这里大致分一下,便于后面总结 1.一般模式 默认打开文件后的模式,这个时候只能移动光标,不能编辑 2.输入模式 一般模式按下i,就可以进入输入模式,输入 ...

  10. perl 二维数组

    perl没有真正的二维数组,所谓的二维数组其实是把一维数组以引用的方式放到另外一个一维数组. 二维数组定义 : my @array1=([1,2],[3,4],[45,9],[66,-5]);     ...