提交 2eb09a7a 编写于 作者: 之一Yo's avatar 之一Yo

添加系统托盘菜单

上级 60605848
# coding:utf-8
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QWidget, QSystemTrayIcon, QHBoxLayout, QLabel
from qfluentwidgets import Action, SystemTrayMenu, MessageBox, setTheme, Theme
class SystemTrayIcon(QSystemTrayIcon):
def __init__(self, parent=None):
super().__init__(parent=parent)
self.setIcon(parent.windowIcon())
self.setToolTip('硝子酱一级棒卡哇伊🥰')
self.menu = SystemTrayMenu(parent=parent)
self.menu.addActions([
Action('🎤 唱'),
Action('🕺 跳'),
Action('🤘🏼 RAP'),
Action('🎶 Music'),
Action('🏀 篮球', triggered=self.ikun),
])
self.setContextMenu(self.menu)
def ikun(self):
content = """巅峰产生虚伪的拥护,黄昏见证真正的使徒 🏀
⠀⠰⢷⢿⠄
⠀⠀⠀⠀⠀⣼⣷⣄
⠀⠀⣤⣿⣇⣿⣿⣧⣿⡄
⢴⠾⠋⠀⠀⠻⣿⣷⣿⣿⡀
⠀⢀⣿⣿⡿⢿⠈⣿
⠀⠀⠀⢠⣿⡿⠁⠀⡊⠀⠙
⠀⠀⠀⢿⣿⠀⠀⠹⣿
⠀⠀⠀⠀⠹⣷⡀⠀⣿⡄
⠀⠀⠀⠀⣀⣼⣿⠀⢈⣧
"""
w = MessageBox(
title='坤家军!集合!',
content=content,
parent=self.parent()
)
w.yesButton.setText('献出心脏')
w.cancelButton.setText('你干嘛~')
w.exec()
class Demo(QWidget):
def __init__(self):
super().__init__()
# setTheme(Theme.DARK)
self.setLayout(QHBoxLayout())
self.label = QLabel('Right-click system tray icon', self)
self.label.setAlignment(Qt.AlignCenter)
self.layout().addWidget(self.label)
self.resize(500, 500)
self.setStyleSheet('Demo{background: white} QLabel{font-size: 20px}')
self.setWindowIcon(QIcon(':/qfluentwidgets/images/logo.png'))
self.systemTrayIcon = SystemTrayIcon(self)
self.systemTrayIcon.show()
if __name__ == '__main__':
# enable dpi scale
QApplication.setHighDpiScaleFactorRoundingPolicy(Qt.HighDpiScaleFactorRoundingPolicy.PassThrough)
QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
QApplication.setAttribute(Qt.AA_UseHighDpiPixmaps)
app = QApplication(sys.argv)
w = Demo()
w.show()
app.exec_()
\ No newline at end of file
...@@ -12,7 +12,7 @@ Examples are available at https://github.com/zhiyiYo/PyQt-Fluent-Widgets/tree/ma ...@@ -12,7 +12,7 @@ Examples are available at https://github.com/zhiyiYo/PyQt-Fluent-Widgets/tree/ma
:license: GPLv3, see LICENSE for more details. :license: GPLv3, see LICENSE for more details.
""" """
__version__ = "1.0.3" __version__ = "1.0.4"
from .components import * from .components import *
from .common import * from .common import *
......
/*设置菜单样式*/ RoundMenu {
QMenu { background-color: transparent;
background-color: rgb(43, 43, 43); border: none;
font: 14px 'Segoe UI', 'Microsoft YaHei', 'PingFang SC';
padding: 4px 0px 4px 0px;
border: 1px solid rgb(34, 34, 34);
color: white;
}
QMenu::right-arrow {
width: 13px;
height: 13px;
right: 13px;
border-image: url(:/qfluentwidgets/images/menu/ChevronRight_white.svg);
}
QMenu::separator {
height: 1px;
background: rgba(255, 255, 255, 104);
margin-right: 13px;
margin-top: 4px;
margin-bottom: 4px;
margin-left: 10px;
}
QMenu::item {
padding: 6px 10px 6px 10px;
}
QMenu::item:selected {
border-width: 1px;
border-color: rgb(33, 33, 33);
background: rgb(64, 64, 64);
color: white;
} }
MenuActionListWidget { MenuActionListWidget {
border: 1px solid rgba(255, 255, 255, 0.1); border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 9px; border-radius: 9px;
......
QMenu { RoundMenu {
background-color: rgb(232, 232, 232); background-color: transparent;
font: 14px 'Segoe UI', 'Microsoft YaHei', 'PingFang SC'; border: none;
padding: 4px 0px 4px 0px;
border: 1px solid rgb(200, 200, 200);
}
QMenu::right-arrow {
width: 13px;
height: 13px;
right: 13px;
border-image: url(:/qfluentwidgets/images/menu/ChevronRight_black.svg);
}
QMenu::separator {
height: 1px;
background: rgba(0, 0, 0, 104);
margin-right: 13px;
margin-top: 4px;
margin-bottom: 4px;
margin-left: 10px;
}
QMenu::item {
padding: 6px 10px 6px 10px;
}
QMenu::item:selected {
border-width: 1px;
border-color: rgb(212, 212, 212);
background: rgba(0, 0, 0, 25);
color: black;
} }
MenuActionListWidget { MenuActionListWidget {
border: 1px solid rgba(0, 0, 0, 0.1); border: 1px solid rgba(0, 0, 0, 0.1);
border-radius: 9px; border-radius: 9px;
......
此差异已折叠。
...@@ -15,7 +15,8 @@ from .label import (PixmapLabel, CaptionLabel, StrongBodyLabel, BodyLabel, Subti ...@@ -15,7 +15,8 @@ from .label import (PixmapLabel, CaptionLabel, StrongBodyLabel, BodyLabel, Subti
LargeTitleLabel, DisplayLabel, FluentLabelBase, ImageLabel) LargeTitleLabel, DisplayLabel, FluentLabelBase, ImageLabel)
from .list_view import ListWidget, ListView, ListItemDelegate from .list_view import ListWidget, ListView, ListItemDelegate
from .menu import (DWMMenu, LineEditMenu, RoundMenu, MenuAnimationManager, MenuAnimationType, IndicatorMenuItemDelegate, from .menu import (DWMMenu, LineEditMenu, RoundMenu, MenuAnimationManager, MenuAnimationType, IndicatorMenuItemDelegate,
MenuItemDelegate, ShortcutMenuItemDelegate, CheckableMenu, MenuIndicatorType) MenuItemDelegate, ShortcutMenuItemDelegate, CheckableMenu, MenuIndicatorType, SystemTrayMenu,
CheckableSystemTrayMenu)
from .info_bar import InfoBar, InfoBarIcon, InfoBarPosition, InfoBarManager from .info_bar import InfoBar, InfoBarIcon, InfoBarPosition, InfoBarManager
from .info_badge import InfoBadge, InfoLevel, DotInfoBadge, IconInfoBadge, InfoBadgePosition, InfoBadgeManager from .info_badge import InfoBadge, InfoLevel, DotInfoBadge, IconInfoBadge, InfoBadgePosition, InfoBadgeManager
from .scroll_area import SingleDirectionScrollArea, SmoothMode, SmoothScrollArea, ScrollArea from .scroll_area import SingleDirectionScrollArea, SmoothMode, SmoothScrollArea, ScrollArea
......
...@@ -246,7 +246,7 @@ class MenuActionListWidget(QListWidget): ...@@ -246,7 +246,7 @@ class MenuActionListWidget(QListWidget):
return sum(self.item(i).sizeHint().height() for i in range(self.count())) return sum(self.item(i).sizeHint().height() for i in range(self.count()))
class RoundMenu(QWidget): class RoundMenu(QMenu):
""" Round corner menu """ """ Round corner menu """
closedSignal = pyqtSignal() closedSignal = pyqtSignal()
...@@ -690,6 +690,18 @@ class RoundMenu(QWidget): ...@@ -690,6 +690,18 @@ class RoundMenu(QWidget):
""" """
self.exec(pos, ani, aniType) self.exec(pos, ani, aniType)
def adjustPosition(self):
m = self.layout().contentsMargins()
rect = QApplication.screenAt(QCursor.pos()).availableGeometry()
w, h = self.layout().sizeHint().width() + 5, self.layout().sizeHint().height()
x = min(self.x() - m.left(), rect.right() - w)
y = self.y()
if y > rect.bottom() - h:
y = self.y() - h + m.bottom()
self.move(x, y)
class MenuAnimationManager(QObject): class MenuAnimationManager(QObject):
""" Menu animation manager """ """ Menu animation manager """
...@@ -1126,3 +1138,19 @@ class CheckableMenu(RoundMenu): ...@@ -1126,3 +1138,19 @@ class CheckableMenu(RoundMenu):
def _adjustItemText(self, item: QListWidgetItem, action: QAction): def _adjustItemText(self, item: QListWidgetItem, action: QAction):
w = super()._adjustItemText(item, action) w = super()._adjustItemText(item, action)
item.setSizeHint(QSize(w + 26, self.itemHeight)) item.setSizeHint(QSize(w + 26, self.itemHeight))
class SystemTrayMenu(RoundMenu):
""" System tray menu """
def showEvent(self, e):
super().showEvent(e)
self.adjustPosition()
class CheckableSystemTrayMenu(CheckableMenu):
""" Checkable system tray menu """
def showEvent(self, e):
super().showEvent(e)
self.adjustPosition()
...@@ -6,7 +6,7 @@ with open('README.md', encoding='utf-8') as f: ...@@ -6,7 +6,7 @@ with open('README.md', encoding='utf-8') as f:
setuptools.setup( setuptools.setup(
name="PyQt-Fluent-Widgets", name="PyQt-Fluent-Widgets",
version="1.0.3", version="1.0.4",
keywords="pyqt fluent widgets", keywords="pyqt fluent widgets",
author="zhiyiYo", author="zhiyiYo",
author_email="shokokawaii@outlook.com", author_email="shokokawaii@outlook.com",
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册