test-scripts: Add a QSystemTrayIcon script.
Michael Webster
2 years ago
Binary diff not shown
Binary diff not shown
0 | #!/usr/bin/env python3 | |
1 | ||
2 | # install python3-pyside2.qtgui, .qtwidgets | |
3 | ||
4 | from PySide2.QtWidgets import QApplication, QSystemTrayIcon, QMenu, QAction | |
5 | from PySide2.QtGui import QIcon, QPixmap | |
6 | ||
7 | icon_type = "theme" | |
8 | tic_toc = False | |
9 | ||
10 | app = QApplication([]) | |
11 | app.setQuitOnLastWindowClosed(False) | |
12 | ||
13 | icon = QIcon.fromTheme("dialog-warning") | |
14 | tray = QSystemTrayIcon() | |
15 | tray.setIcon(icon) | |
16 | ||
17 | tray.setVisible(True) | |
18 | ||
19 | ||
20 | def use_icon_theme(item): | |
21 | global icon_type | |
22 | icon_type = "theme" | |
23 | ||
24 | def use_icon_file(item): | |
25 | global icon_type | |
26 | icon_type = "file" | |
27 | ||
28 | def use_icon_pixels(item): | |
29 | global icon_type | |
30 | icon_type = "pixels" | |
31 | ||
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) | |
48 | ||
49 | menu = QMenu() | |
50 | ||
51 | entry = "Use icon name" | |
52 | action = QAction(entry) | |
53 | action.triggered.connect(use_icon_theme) | |
54 | menu.addAction(action) | |
55 | ||
56 | entry = "Use icon file" | |
57 | action2 = QAction(entry) | |
58 | action2.triggered.connect(use_icon_file) | |
59 | menu.addAction(action2) | |
60 | ||
61 | entry = "Use icon pixels" | |
62 | action3 = QAction(entry) | |
63 | action3.triggered.connect(use_icon_pixels) | |
64 | menu.addAction(action3) | |
65 | ||
66 | entry = "Quit" | |
67 | action4 = QAction(entry) | |
68 | action4.triggered.connect(app.quit) | |
69 | menu.addAction(action4) | |
70 | ||
71 | tray.setContextMenu(menu) | |
72 | tray.activated.connect(icon_activated) | |
73 | ||
74 | app.exec_()⏎ |