Skip to content

Commit

Permalink
系统托盘闪烁
Browse files Browse the repository at this point in the history
  • Loading branch information
892768447 committed Dec 9, 2021
1 parent 48ef9a5 commit bcd4f71
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
4 changes: 4 additions & 0 deletions QSystemTrayIcon/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- 目录
- [最小化到系统托盘](#1最小化到系统托盘)
- [系统托盘闪烁](#2系统托盘闪烁)

## 1、最小化到系统托盘

Expand All @@ -13,5 +14,8 @@
![MinimizeToTray](ScreenShot/MinimizeToTray.gif)

## 2、系统托盘闪烁

[运行 TrayNotify.py](TrayNotify.py)

通过定时器设置不同图标来实现闪烁。
71 changes: 71 additions & 0 deletions QSystemTrayIcon/TrayNotify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2021年12月09日
@author: Irony
@site: https://pyqt.site , https://github.com/PyQt5
@email: [email protected]
@file: TrayNotify
@description: 托盘闪烁
"""

try:
from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import (QApplication, QHBoxLayout, QPushButton,
QStyle, QSystemTrayIcon, QWidget)
except ImportError:
from PySide2.QtCore import QTimer
from PySide2.QtWidgets import (QApplication, QHBoxLayout, QPushButton,
QStyle, QSystemTrayIcon, QWidget)


class Window(QWidget):

def __init__(self, *args, **kwargs):
super(Window, self).__init__(*args, **kwargs)
layout = QHBoxLayout(self)
layout.addWidget(QPushButton('开始闪烁', self, clicked=self.start_flash))
layout.addWidget(QPushButton('停止闪烁', self, clicked=self.stop_flash))
# 创建托盘图标
self.tray_icon = QSystemTrayIcon(self)
self.tray_icon.setIcon(self.style().standardIcon(
QStyle.SP_ComputerIcon))
self.tray_icon.show()
# 图标闪烁定时器
self.tray_visible = True
self.flash_timer = QTimer(self, timeout=self.flash_icon)

def closeEvent(self, event):
self.stop_flash()
self.tray_icon.hide()
super(Window, self).closeEvent(event)

def start_flash(self):
"""开始闪烁"""
if not self.flash_timer.isActive():
self.flash_timer.start(500)

def stop_flash(self):
"""停止闪烁后需要显示图标"""
if self.flash_timer.isActive():
self.flash_timer.stop()
self.tray_icon.setIcon(self.style().standardIcon(
QStyle.SP_ComputerIcon))

def flash_icon(self):
"""根据当前图标是否可见切换图标"""
if self.tray_visible:
self.tray_icon.setIcon(self.style().standardIcon(
QStyle.SP_TrashIcon))
else:
self.tray_icon.setIcon(self.style().standardIcon(
QStyle.SP_ComputerIcon))
self.tray_visible = not self.tray_visible


if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ https://pyqt.site 论坛是专门针对PyQt5学习和提升开设的网站,分
- [边框阴影动画](QGraphicsDropShadowEffect/ShadowEffect.py)
- [QSystemTrayIcon](QSystemTrayIcon)
- [最小化到系统托盘](QSystemTrayIcon/MinimizeToTray.py)
- [QSystemTrayIcon](QSystemTrayIcon)
- [系统托盘闪烁](QSystemTrayIcon/TrayNotify.py)

- [Demo](Demo)
- [重启窗口Widget](Demo/RestartWindow.py)
Expand Down

0 comments on commit bcd4f71

Please sign in to comment.