-
-
Notifications
You must be signed in to change notification settings - Fork 2k
增加了 QSystemTrayIcon,用于将软件最小化到系统托盘(Tray) #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
QSystemTrayIcon/MinimizeToTray.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import sys | ||
|
||
from PyQt5.QtWidgets import ( | ||
QApplication, QMainWindow, | ||
QLabel, QGridLayout, QWidget, | ||
QCheckBox, QSystemTrayIcon, | ||
QSpacerItem, QSizePolicy, QMenu, QAction, QStyle, qApp) | ||
from PyQt5.QtCore import QSize | ||
|
||
|
||
class MainWindow(QMainWindow): | ||
""" | ||
Сheckbox and system tray icons. | ||
Will initialize in the constructor. | ||
""" | ||
check_box = None | ||
tray_icon = None | ||
|
||
# Override the class constructor | ||
def __init__(self): | ||
# Be sure to call the super class method | ||
QMainWindow.__init__(self) | ||
|
||
self.setMinimumSize(QSize(480, 80)) # Set sizes | ||
self.setWindowTitle("System Tray Application") # Set a title | ||
# Create a central widget | ||
central_widget = QWidget(self) | ||
# Set the central widget | ||
self.setCentralWidget(central_widget) | ||
|
||
grid_layout = QGridLayout(self) # Create a QGridLayout | ||
# Set the layout into the central widget | ||
central_widget.setLayout(grid_layout) | ||
grid_layout.addWidget( | ||
QLabel("Application, which can minimize to Tray", self), 0, 0) | ||
|
||
# Add a checkbox, which will depend on the behavior of the program when the window is closed | ||
self.check_box = QCheckBox('Minimize to Tray') | ||
grid_layout.addWidget(self.check_box, 1, 0) | ||
grid_layout.addItem(QSpacerItem( | ||
0, 0, QSizePolicy.Expanding, QSizePolicy.Expanding), 2, 0) | ||
|
||
# Init QSystemTrayIcon | ||
self.tray_icon = QSystemTrayIcon(self) | ||
self.tray_icon.setIcon( | ||
self.style().standardIcon(QStyle.SP_ComputerIcon)) | ||
|
||
''' | ||
Define and add steps to work with the system tray icon | ||
show - show window | ||
hide - hide window | ||
exit - exit from application | ||
''' | ||
show_action = QAction("Show", self) | ||
quit_action = QAction("Exit", self) | ||
hide_action = QAction("Hide", self) | ||
show_action.triggered.connect(self.show) | ||
hide_action.triggered.connect(self.hide) | ||
quit_action.triggered.connect(qApp.quit) | ||
tray_menu = QMenu() | ||
tray_menu.addAction(show_action) | ||
tray_menu.addAction(hide_action) | ||
tray_menu.addAction(quit_action) | ||
self.tray_icon.setContextMenu(tray_menu) | ||
self.tray_icon.show() | ||
|
||
# Override closeEvent, to intercept the window closing event | ||
# The window will be closed only if there is no check mark in the check box | ||
def closeEvent(self, event): | ||
if self.check_box.isChecked(): | ||
event.ignore() | ||
self.hide() | ||
self.tray_icon.showMessage( | ||
"Tray Program", | ||
"Application was minimized to Tray", | ||
QSystemTrayIcon.Information, | ||
2000 | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
app = QApplication(sys.argv) | ||
mw = MainWindow() | ||
mw.show() | ||
sys.exit(app.exec()) |
21 changes: 21 additions & 0 deletions
QSystemTrayIcon/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# QSystemTrayIcon | ||
|
||
- 目录 | ||
- [最小化到系统托盘](#1、最小化到系统托盘) | ||
|
||
|
||
|
||
## 1、最小化到系统托盘 | ||
|
||
[运行 MinimizeToTray.py](MinimizeToTray.py) | ||
|
||
选择 Minimize to Tray 在关闭窗口时最小化到系统托盘。 | ||
|
||
> Reference: | ||
> | ||
> [PyQt5 - Lesson 003. QSystemTrayIcon - How to minimize application to tray]: https://evileg.com/en/post/68/ | ||
|
||
 | ||
|
||
|
||
|
Binary file added
QSystemTrayIcon/ScreenShot/MinimizeToTray.gif
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.