PyQt/QtWinExtras/ThumbnailToolBar.py

94 lines
3.4 KiB
Python
Raw Normal View History

2020-07-04 00:08:06 +08:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2020/7/3
@author: Irony
2021-07-13 14:52:26 +08:00
@site: https://pyqt.site , https://github.com/PyQt5
2020-07-04 00:08:06 +08:00
@email: 892768447@qq.com
@file: ThumbnailToolBar
@description:
"""
import cgitb
import sys
2021-07-13 14:52:26 +08:00
try:
from PyQt5.QtWidgets import QWidget, QApplication, QLabel, QStyle, QVBoxLayout
from PyQt5.QtWinExtras import QWinThumbnailToolBar, QWinThumbnailToolButton
except ImportError:
from PySide2.QtWidgets import QWidget, QApplication, QLabel, QStyle, QVBoxLayout
from PySide2.QtWinExtras import QWinThumbnailToolBar, QWinThumbnailToolButton
2020-07-04 00:08:06 +08:00
class Window(QWidget):
def __init__(self, *args, **kwargs):
super(Window, self).__init__(*args, **kwargs)
self.countPrev = 0
self.countNext = 0
self.init_ui()
def init_ui(self):
layout = QVBoxLayout(self)
self.labelPrev = QLabel(self)
self.labelControl = QLabel('暂停播放', self)
self.labelNext = QLabel(self)
layout.addWidget(self.labelPrev)
layout.addWidget(self.labelControl)
layout.addWidget(self.labelNext)
# 任务栏缩略图工具条
self.toolBar = QWinThumbnailToolBar(self)
# 上一首,播放/暂停,下一首按钮
self.toolBtnPrev = QWinThumbnailToolButton(self.toolBar)
self.toolBtnPrev.setToolTip('上一首')
self.toolBtnPrev.setIcon(self.style().standardIcon(QStyle.SP_MediaSkipBackward))
self.toolBtnPrev.clicked.connect(self.set_prev)
self.toolBar.addButton(self.toolBtnPrev)
self.toolBtnControl = QWinThumbnailToolButton(self.toolBar)
self.toolBtnControl.setToolTip('播放')
self.toolBtnControl.setProperty('status', 0)
self.toolBtnControl.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay))
self.toolBtnControl.clicked.connect(self.set_control)
self.toolBar.addButton(self.toolBtnControl)
self.toolBtnNext = QWinThumbnailToolButton(self.toolBar)
self.toolBtnNext.setToolTip('下一首')
self.toolBtnNext.setIcon(self.style().standardIcon(QStyle.SP_MediaSkipForward))
self.toolBtnNext.clicked.connect(self.set_next)
self.toolBar.addButton(self.toolBtnNext)
def set_prev(self):
self.countPrev += 1
self.labelPrev.setText('点击上一首按钮: %d' % self.countPrev)
def set_next(self):
self.countNext += 1
self.labelNext.setText('点击下一首按钮: %d' % self.countNext)
def set_control(self):
if self.toolBtnControl.property('status') == 0:
self.labelControl.setText('正在播放')
self.toolBtnControl.setProperty('status', 1)
self.toolBtnControl.setIcon(self.style().standardIcon(QStyle.SP_MediaPause))
else:
self.labelControl.setText('暂停播放')
self.toolBtnControl.setProperty('status', 0)
self.toolBtnControl.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay))
def showEvent(self, event):
super(Window, self).showEvent(event)
if not self.toolBar.window():
# 必须等窗口显示后设置才有效,或者通过软件流程在适当的时候设置也可以
self.toolBar.setWindow(self.windowHandle())
if __name__ == '__main__':
2021-07-13 14:52:26 +08:00
cgitb.enable(format='text')
2020-07-04 00:08:06 +08:00
app = QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())