|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +""" |
| 5 | +Created on 2020/7/3 |
| 6 | +@author: Irony |
| 7 | +@site: https://pyqt.site https://github.com/PyQt5 |
| 8 | + |
| 9 | +@file: ThumbnailToolBar |
| 10 | +@description: |
| 11 | +""" |
| 12 | + |
| 13 | +__Author__ = 'Irony' |
| 14 | +__Copyright__ = 'Copyright (c) 2020' |
| 15 | +__Version__ = 'Version 1.0' |
| 16 | + |
| 17 | +import cgitb |
| 18 | +import sys |
| 19 | + |
| 20 | +from PyQt5.QtWidgets import QWidget, QApplication, QLabel, QStyle, QVBoxLayout |
| 21 | +from PyQt5.QtWinExtras import QWinThumbnailToolBar, QWinThumbnailToolButton |
| 22 | + |
| 23 | + |
| 24 | +class Window(QWidget): |
| 25 | + |
| 26 | + def __init__(self, *args, **kwargs): |
| 27 | + super(Window, self).__init__(*args, **kwargs) |
| 28 | + self.countPrev = 0 |
| 29 | + self.countNext = 0 |
| 30 | + self.init_ui() |
| 31 | + |
| 32 | + def init_ui(self): |
| 33 | + layout = QVBoxLayout(self) |
| 34 | + self.labelPrev = QLabel(self) |
| 35 | + self.labelControl = QLabel('暂停播放', self) |
| 36 | + self.labelNext = QLabel(self) |
| 37 | + layout.addWidget(self.labelPrev) |
| 38 | + layout.addWidget(self.labelControl) |
| 39 | + layout.addWidget(self.labelNext) |
| 40 | + |
| 41 | + # 任务栏缩略图工具条 |
| 42 | + self.toolBar = QWinThumbnailToolBar(self) |
| 43 | + # 上一首,播放/暂停,下一首按钮 |
| 44 | + self.toolBtnPrev = QWinThumbnailToolButton(self.toolBar) |
| 45 | + self.toolBtnPrev.setToolTip('上一首') |
| 46 | + self.toolBtnPrev.setIcon(self.style().standardIcon(QStyle.SP_MediaSkipBackward)) |
| 47 | + self.toolBtnPrev.clicked.connect(self.set_prev) |
| 48 | + self.toolBar.addButton(self.toolBtnPrev) |
| 49 | + |
| 50 | + self.toolBtnControl = QWinThumbnailToolButton(self.toolBar) |
| 51 | + self.toolBtnControl.setToolTip('播放') |
| 52 | + self.toolBtnControl.setProperty('status', 0) |
| 53 | + self.toolBtnControl.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay)) |
| 54 | + self.toolBtnControl.clicked.connect(self.set_control) |
| 55 | + self.toolBar.addButton(self.toolBtnControl) |
| 56 | + |
| 57 | + self.toolBtnNext = QWinThumbnailToolButton(self.toolBar) |
| 58 | + self.toolBtnNext.setToolTip('下一首') |
| 59 | + self.toolBtnNext.setIcon(self.style().standardIcon(QStyle.SP_MediaSkipForward)) |
| 60 | + self.toolBtnNext.clicked.connect(self.set_next) |
| 61 | + self.toolBar.addButton(self.toolBtnNext) |
| 62 | + |
| 63 | + def set_prev(self): |
| 64 | + self.countPrev += 1 |
| 65 | + self.labelPrev.setText('点击上一首按钮: %d 次' % self.countPrev) |
| 66 | + |
| 67 | + def set_next(self): |
| 68 | + self.countNext += 1 |
| 69 | + self.labelNext.setText('点击下一首按钮: %d 次' % self.countNext) |
| 70 | + |
| 71 | + def set_control(self): |
| 72 | + if self.toolBtnControl.property('status') == 0: |
| 73 | + self.labelControl.setText('正在播放') |
| 74 | + self.toolBtnControl.setProperty('status', 1) |
| 75 | + self.toolBtnControl.setIcon(self.style().standardIcon(QStyle.SP_MediaPause)) |
| 76 | + else: |
| 77 | + self.labelControl.setText('暂停播放') |
| 78 | + self.toolBtnControl.setProperty('status', 0) |
| 79 | + self.toolBtnControl.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay)) |
| 80 | + |
| 81 | + def showEvent(self, event): |
| 82 | + super(Window, self).showEvent(event) |
| 83 | + if not self.toolBar.window(): |
| 84 | + # 必须等窗口显示后设置才有效,或者通过软件流程在适当的时候设置也可以 |
| 85 | + self.toolBar.setWindow(self.windowHandle()) |
| 86 | + |
| 87 | + |
| 88 | +if __name__ == '__main__': |
| 89 | + cgitb.enable(1, None, 5, '') |
| 90 | + app = QApplication(sys.argv) |
| 91 | + w = Window() |
| 92 | + w.show() |
| 93 | + sys.exit(app.exec_()) |
0 commit comments