class PlayProgressBar(QWidget): """ 歌曲播放进度条 """ def __init__(self, duration: str = '0:00', parent=None): super().__init__(parent) # 创建两个标签和一个进度条 self.progressSlider = Slider(Qt.Horizontal, self) self.currentTimeLabel = QLabel('0:00', self) self.totalTimeLabel = QLabel(duration, self) # 初始化界面 self.__initWidget() self.__setQss() def __initWidget(self): """ 初始化小部件 """ self.setFixedHeight(38) self.progressSlider.move(73, 0) self.currentTimeLabel.move(33, 9) self.progressSlider.setObjectName('progressSlider') self.currentTimeLabel.setObjectName('timeLabel') self.totalTimeLabel.setObjectName('timeLabel') def setCurrentTime(self, currentTime): """ 更新当前时间标签,currentTime的单位为ms """ seconds, minutes = self.getSecondMinute(currentTime) self.currentTimeLabel.setText(f'{minutes}:{str(seconds).rjust(2,"0")}') self.currentTimeLabel.move( 33 - 9 * (len(self.totalTimeLabel.text()) - 4), 9) def setTotalTime(self, totalTime): """ 更新总时长标签,totalTime的单位为ms """ seconds, minutes = self.getSecondMinute(totalTime) self.totalTimeLabel.setText(f'{minutes}:{str(seconds).rjust(2,"0")}') def getSecondMinute(self, time): """ 将毫秒转换为分和秒 """ seconds = int(time / 1000) minutes = seconds // 60 seconds -= minutes * 60 return seconds, minutes def __setQss(self): """ 设置层叠样式 """ with open('resource\\css\\playProgressBar.qss', encoding='utf-8') as f: self.setStyleSheet(f.read()) def resizeEvent(self, e): """ 改变尺寸时拉伸进度条 """ self.progressSlider.resize(self.width() - 146, 38) self.totalTimeLabel.move(self.width() - 57, 10) super().resizeEvent(e)
class RightWidgetGroup(QWidget): """ 播放按钮组 """ def __init__(self, parent=None): super().__init__(parent) # 创建小部件 self.volumeButton = VolumeButton(self) self.volumeSlider = Slider(Qt.Horizontal, self) self.smallPlayModeButton = BasicButton( r'resource\images\playBar\最小播放模式_45_45.png', self) self.moreActionsButton = BasicButton( r'resource\images\playBar\更多操作_45_45.png', self) self.widget_list = [ self.volumeButton, self.volumeSlider, self.smallPlayModeButton, self.moreActionsButton ] # 创建布局 self.h_layout = QHBoxLayout() # 初始化界面 self.__initWidget() self.__initLayout() def __initWidget(self): """ 初始化小部件 """ self.setFixedSize(301, 16 + 67) self.volumeSlider.setRange(0, 100) self.volumeSlider.setObjectName('volumeSlider') # 将音量滑动条数值改变信号连接到槽函数 self.volumeSlider.setValue(20) def __initLayout(self): """ 初始化布局 """ self.__spacing_list = [7, 8, 8, 5, 7] self.h_layout.setSpacing(0) self.h_layout.setContentsMargins(0, 0, 0, 0) # 将小部件添加到布局中 for i in range(4): self.h_layout.addSpacing(self.__spacing_list[i]) self.h_layout.addWidget(self.widget_list[i]) else: self.h_layout.addSpacing(self.__spacing_list[-1]) self.setLayout(self.h_layout)