| 29 | |
| 30 | # Override the class constructor |
| 31 | def __init__(self): |
| 32 | # Be sure to call the super class method |
| 33 | QMainWindow.__init__(self) |
| 34 | |
| 35 | self.setMinimumSize(QSize(480, 80)) # Set sizes |
| 36 | self.setWindowTitle("System Tray Application") # Set a title |
| 37 | # Create a central widget |
| 38 | central_widget = QWidget(self) |
| 39 | # Set the central widget |
| 40 | self.setCentralWidget(central_widget) |
| 41 | |
| 42 | grid_layout = QGridLayout(self) # Create a QGridLayout |
| 43 | # Set the layout into the central widget |
| 44 | central_widget.setLayout(grid_layout) |
| 45 | grid_layout.addWidget( |
| 46 | QLabel("Application, which can minimize to Tray", self), 0, 0) |
| 47 | |
| 48 | # Add a checkbox, which will depend on the behavior of the program when the window is closed |
| 49 | self.check_box = QCheckBox('Minimize to Tray') |
| 50 | grid_layout.addWidget(self.check_box, 1, 0) |
| 51 | grid_layout.addItem(QSpacerItem( |
| 52 | 0, 0, QSizePolicy.Expanding, QSizePolicy.Expanding), 2, 0) |
| 53 | |
| 54 | # Init QSystemTrayIcon |
| 55 | self.tray_icon = QSystemTrayIcon(self) |
| 56 | self.tray_icon.setIcon( |
| 57 | self.style().standardIcon(QStyle.SP_ComputerIcon)) |
| 58 | |
| 59 | ''' |
| 60 | Define and add steps to work with the system tray icon |
| 61 | show - show window |
| 62 | hide - hide window |
| 63 | exit - exit from application |
| 64 | ''' |
| 65 | show_action = QAction("Show", self) |
| 66 | quit_action = QAction("Exit", self) |
| 67 | hide_action = QAction("Hide", self) |
| 68 | show_action.triggered.connect(self.show) |
| 69 | hide_action.triggered.connect(self.hide) |
| 70 | quit_action.triggered.connect(QApplication.instance().quit) |
| 71 | tray_menu = QMenu() |
| 72 | tray_menu.addAction(show_action) |
| 73 | tray_menu.addAction(hide_action) |
| 74 | tray_menu.addAction(quit_action) |
| 75 | self.tray_icon.setContextMenu(tray_menu) |
| 76 | self.tray_icon.show() |
| 77 | |
| 78 | # Override closeEvent, to intercept the window closing event |
| 79 | # The window will be closed only if there is no check mark in the check box |