PySide6 GUI 编程(49): 托盘程序QSystemTrayIcon

2024-09-19 22:31:29 浏览数 (3)

示例代码

代码语言:python代码运行次数:0复制
from __future__ import annotations

import sys
from random import randint

from PySide6.QtCore import Slot
from PySide6.QtGui import QAction, QIcon
from PySide6.QtWidgets import QApplication, QMenu, QSystemTrayIcon


@Slot()
def demo_tray_action():
    print('demo tray action', randint(999, 99999))


if __name__ == "__main__":
    app = QApplication(sys.argv)
    app.setQuitOnLastWindowClosed(False)

    tray = QSystemTrayIcon()
    tray.setIcon(QIcon('/xxxxxx/ubuntu-plain-icon.svg'))
    tray.setVisible(True)
    tray.setToolTip('a tray app with ubuntu icon')

    menu = QMenu()
    action = QAction('DemoTrayAction')
    action.triggered.connect(demo_tray_action)
    menu.addAction(action)

    tray_quit = QAction('TrayAppQuit')
    tray_quit.triggered.connect(app.quit)
    menu.addAction(tray_quit)

    tray.setContextMenu(menu)

    app.exec()

运行效果

托盘程序运行效果托盘程序运行效果
托盘程序提示信息托盘程序提示信息

0 人点赞