PyQt implements system tray QSystemTrayIcon

Development environment: ubuntu16.04 + PyQt5.14

Official information: https://doc.qt.io/qt-5/desktop-integration.html

QSystemTrayIcon

The QSystemTrayIcon class provides an icon for the application in the system tray.

Modern operating systems usually provide a special area on the desktop called the system tray or notification area. Long-running applications can display icons and short messages.

QDesktopServices

The functions provided by the QDesktopServices class are used to access common desktop services.

Many desktop environments provide a series of services that can perform common tasks through applications. Not only can you open a local browser, but you can also open local files (folders), etc., and you can get directories such as desktop and Home.


Example

The main function:

  • Close the last window without exiting the program
  • Tray menu display: about, log, open, exit four menus
  • About: open the about dialog
  • Log: View log
  • Open: open the main interface
  • Exit: Exit the program
  • Main page: Click the button of connection, power-on, login, etc., and the tray message displays prompt information 

running result:

 Source code:

Code structure:

├── gui

│   ├── mainwindow.py

│   ├── ui_mainwindow.py

│ └── ui_mainwindow.ui

├── icon

│   ├── about.png

│   ├── icon.png

│   └── RR1582*1327.png

├── image.qrc

├── image_rc.py

├── runWin.py

Core code:

mainwindow.py

from PyQt5.QtCore import QCoreApplication, QDateTime, QUrl
from PyQt5.QtGui import QIcon, QPixmap, QDesktopServices
from PyQt5.QtWidgets import QMainWindow, QAction, QMenu, QSystemTrayIcon, QApplication, QMessageBox

from gui.ui_mainwindow import Ui_MainWindow
from image_rc import *

class MainWidget(Ui_MainWindow, QMainWindow):

    def __init__(self, parent=None):
        super(MainWidget, self).__init__(parent)
        self.setupUi(self)
        self.setWindowTitle("桌面集成Python版本")
        self.setWindowIcon(QIcon(":/icon/RR1582*1327.png"))

        self.logAction = QAction("日志", self)
        self.logAction.setIcon(QIcon.fromTheme("document-new"))
        self.aboutAction = QAction("关于", self)
        self.aboutAction.setIcon(QIcon.fromTheme("help-about"))
        self.openAction = QAction("打开", self)
        self.openAction.setIcon(QIcon.fromTheme("media-record"))
        self.quitAction = QAction("退出", self)
        self.quitAction.setIcon(QIcon.fromTheme("application-exit"))  # 从系统主题获取图标

        self.trayIconMenu = QMenu(self)
        self.trayIconMenu.addAction(self.aboutAction)
        self.trayIconMenu.addAction(self.logAction)
        self.trayIconMenu.addSeparator()
        self.trayIconMenu.addAction(self.openAction)
        self.trayIconMenu.addAction(self.quitAction)

        self.trayIcon = QSystemTrayIcon(self)
        self.trayIcon.setContextMenu(self.trayIconMenu)
        self.trayIcon.setIcon(QIcon(":/icon/icon.png"))
        self.trayIcon.setToolTip("xxx")
        self.trayIcon.show()

        self.initWidget()
        self.initConnect()
        self.initLog()

    def initWidget(self):
        # 居中
        desktop = QApplication.desktop()
        current_screen = desktop.screenNumber(self)   # 获取程序所在屏幕是第几个屏幕
        rect = desktop.screenGeometry(current_screen) # 获取程序所在屏幕的尺寸
        self.move((rect.width() - self.width())/2, (rect.height() - self.height())/2)

    def initConnect(self):
        self.quitAction.triggered.connect(self.quitApp)
        self.openAction.triggered.connect(self.openApp)
        self.aboutAction.triggered.connect(self.showAbout)
        self.logAction.triggered.connect(self.openLog)
        self.connectBtn.clicked.connect(self.showConnect)
        self.powerBtn.clicked.connect(self.showPower)
        self.loginBtn.clicked.connect(self.showLogin)

    def initLog(self):
        currentDateTime = QDateTime.currentDateTime()
        time = currentDateTime.toString("yyyy/MM/dd HH:mm:ss")
        f = open("./log.txt", "a")
        f.write(time)
        f.close()

    def quitApp(self):
        QCoreApplication.quit()

    def openApp(self):
        self.showNormal()

    def showAbout(self):
        msg = QMessageBox(self)
        msg.setWindowTitle("关于")
        msg.setText("桌面集成python实现\n \nCopyright © 2020-2020 JUN. \nAll Rights Reserved. ")
        msg.setIconPixmap(QPixmap(":/icon/icon.png"))
        msg.addButton("确定", QMessageBox.ActionRole)
        msg.exec()

    def openLog(self):
        QDesktopServices.openUrl(QUrl("./log.txt"))

    def closeEvent(self, event):
        if self.trayIcon.isVisible():
            QMessageBox.information(
                self, "系统托盘", "程序将继续在系统托盘中运行。要终止该程序,请在系统托盘条目的上下文菜单中选择[退出]。")

    def showConnect(self):
        self.showMessage("连接", "已经连接机械手")

    def showPower(self):
        self.showMessage("上电", "机械手已上电")

    def showLogin(self):
        self.showMessage("权限", "切换权限USER1")

    def showMessage(self, title, content):
        self.trayIcon.showMessage(title, content, QSystemTrayIcon.Information, 1000)

 

runWin.py

import sys
from PyQt5.QtWidgets import QApplication
from gui.mainwindow import MainWidget

if __name__ == '__main__':
    app = QApplication(sys.argv)
    QApplication.setQuitOnLastWindowClosed(False)  # 关闭最后一个窗口不退出程序
    window = MainWidget()
    window.show()
    sys.exit(app.exec_())

 

Guess you like

Origin blog.csdn.net/qq_40602000/article/details/109276102