-
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
TrayNotify.py
71 lines (61 loc) · 2.25 KB
/
TrayNotify.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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_())