qt-system-tray.py: use a class.
Michael Webster
2 years ago
4 | 4 | from PySide2.QtWidgets import QApplication, QSystemTrayIcon, QMenu, QAction |
5 | 5 | from PySide2.QtGui import QIcon, QPixmap |
6 | 6 | |
7 | icon_type = "theme" | |
8 | tic_toc = False | |
7 | class App(QApplication): | |
8 | def __init__(self): | |
9 | super(App, self).__init__([]) | |
9 | 10 | |
10 | app = QApplication([]) | |
11 | app.setQuitOnLastWindowClosed(False) | |
11 | self.icon_type = "theme" | |
12 | self.tic_toc = False | |
12 | 13 | |
13 | icon = QIcon.fromTheme("dialog-warning") | |
14 | tray = QSystemTrayIcon() | |
15 | tray.setIcon(icon) | |
14 | self.setQuitOnLastWindowClosed(False) | |
16 | 15 | |
17 | tray.setVisible(True) | |
16 | self.tray = QSystemTrayIcon() | |
17 | self.tray.setIcon(QIcon.fromTheme("dialog-warning")) | |
18 | self.tray.setVisible(True) | |
18 | 19 | |
20 | self.menu = QMenu() | |
19 | 21 | |
20 | def use_icon_theme(item): | |
21 | global icon_type | |
22 | icon_type = "theme" | |
22 | entry = "Use icon name" | |
23 | action = QAction(entry) | |
24 | action.triggered.connect(self.use_icon_theme) | |
25 | self.menu.addAction(action) | |
23 | 26 | |
24 | def use_icon_file(item): | |
25 | global icon_type | |
26 | icon_type = "file" | |
27 | entry = "Use icon file" | |
28 | action2 = QAction(entry) | |
29 | action2.triggered.connect(self.use_icon_file) | |
30 | self.menu.addAction(action2) | |
27 | 31 | |
28 | def use_icon_pixels(item): | |
29 | global icon_type | |
30 | icon_type = "pixels" | |
32 | entry = "Use icon pixels" | |
33 | action3 = QAction(entry) | |
34 | action3.triggered.connect(self.use_icon_pixels) | |
35 | self.menu.addAction(action3) | |
31 | 36 | |
32 | def icon_activated(reason): | |
33 | global tic_toc | |
34 | global icon_type | |
35 | if icon_type == "theme": | |
36 | icon = QIcon.fromTheme("dialog-warning" if tic_toc else "dialog-error") | |
37 | tic_toc = not tic_toc | |
38 | tray.setIcon(icon) | |
39 | elif icon_type == "file": | |
40 | icon = QIcon("./dialog-warning.png" if tic_toc else "./dialog-error.png") | |
41 | tic_toc = not tic_toc | |
42 | tray.setIcon(icon) | |
43 | else: | |
44 | pixmap = QPixmap("./dialog-warning.png" if tic_toc else "./dialog-error.png") | |
45 | tic_toc = not tic_toc | |
46 | icon = QIcon(pixmap) | |
47 | tray.setIcon(icon) | |
37 | entry = "Quit" | |
38 | action4 = QAction(entry) | |
39 | action4.triggered.connect(self.quit) | |
40 | self.menu.addAction(action4) | |
48 | 41 | |
49 | menu = QMenu() | |
42 | self.tray.setContextMenu(self.menu) | |
43 | self.tray.activated.connect(self.icon_activated) | |
44 | self.exec_() | |
50 | 45 | |
51 | entry = "Use icon name" | |
52 | action = QAction(entry) | |
53 | action.triggered.connect(use_icon_theme) | |
54 | menu.addAction(action) | |
46 | def use_icon_theme(self, item): | |
47 | self.icon_type = "theme" | |
55 | 48 | |
56 | entry = "Use icon file" | |
57 | action2 = QAction(entry) | |
58 | action2.triggered.connect(use_icon_file) | |
59 | menu.addAction(action2) | |
49 | def use_icon_file(self, item): | |
50 | self.icon_type = "file" | |
60 | 51 | |
61 | entry = "Use icon pixels" | |
62 | action3 = QAction(entry) | |
63 | action3.triggered.connect(use_icon_pixels) | |
64 | menu.addAction(action3) | |
52 | def use_icon_pixels(self, item): | |
53 | self.icon_type = "pixels" | |
65 | 54 | |
66 | entry = "Quit" | |
67 | action4 = QAction(entry) | |
68 | action4.triggered.connect(app.quit) | |
69 | menu.addAction(action4) | |
55 | def icon_activated(self, reason): | |
56 | if self.icon_type == "theme": | |
57 | icon = QIcon.fromTheme("dialog-warning" if self.tic_toc else "dialog-error") | |
58 | self.tray.setIcon(icon) | |
59 | self.tic_toc = not self.tic_toc | |
60 | elif self.icon_type == "file": | |
61 | icon = QIcon("./dialog-warning.png" if self.tic_toc else "./dialog-error.png") | |
62 | self.tray.setIcon(icon) | |
63 | self.tic_toc = not self.tic_toc | |
64 | else: | |
65 | pixmap = QPixmap("./dialog-warning.png" if self.tic_toc else "./dialog-error.png") | |
66 | icon = QIcon(pixmap) | |
67 | self.tray.setIcon(icon) | |
68 | self.tic_toc = not self.tic_toc | |
70 | 69 | |
71 | tray.setContextMenu(menu) | |
72 | tray.activated.connect(icon_activated) | |
73 | ||
74 | app.exec_()⏎ | |
70 | if __name__ == '__main__': | |
71 | app = App() |