-
Notifications
You must be signed in to change notification settings - Fork 18
/
TrayIcon.py
53 lines (45 loc) · 2.22 KB
/
TrayIcon.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
from PyQt5 import QtCore, QtWidgets, QtGui
from MyselfClose import MyselfClose
class TrayIcon(QtWidgets.QSystemTrayIcon):
def __init__(self, MainWindow, parent=None):
super(TrayIcon, self).__init__(parent)
self.ui = MainWindow
self.menu = QtWidgets.QMenu()
self.show_window_action = QtWidgets.QAction("啟動", self, triggered=self.show_window)
self.show_msg_action = QtWidgets.QAction("顯示通知", self, triggered=self.showMsg)
self.quit_action = QtWidgets.QAction("退出", self, triggered=self.quit)
self.menu.addAction(self.show_window_action)
self.menu.addAction(self.show_msg_action)
self.menu.addAction(self.quit_action)
self.setContextMenu(self.menu)
self.setIcon(QtGui.QIcon('./image/logo.ico'))
self.icon = QtGui.QIcon('./image/logo.ico')
self.activated.connect(self.onIconClicked)
def showMsg(self):
self.showMessage("Message", "幫我此專案 GitHub 按星星~ 謝謝您!", self.icon)
def show_window(self):
self.ui.showNormal()
self.ui.activateWindow()
def quit(self):
reply = QtWidgets.QMessageBox.question(self.ui,
'關閉',
"是否離開程式?",
QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No,
QtWidgets.QMessageBox.No)
if reply == QtWidgets.QMessageBox.Yes:
self.myself_close = MyselfClose(anime=self.ui)
self.myself_close.show()
# 點擊 icon 傳送的信號會有一個整數值,1=右键,2=連續兩下左鍵,3=單點左鍵,4=滾輪點擊
def onIconClicked(self, reason):
if reason == 2 or reason == 3:
if self.ui.isMinimized() or not self.ui.isVisible():
self.ui.showNormal()
self.ui.activateWindow()
self.ui.setWindowFlags(QtCore.Qt.Window)
self.ui.show()
self.hide()
else:
self.ui.showMinimized()
self.ui.setWindowFlags(QtCore.Qt.SplashScreen)
self.ui.show()
self.hide()