赞
踩
QToolButton工具按钮, 最常用于QToolBar中,不同于QPushButton, QToolButton通常不显示文本标签,而只显示一个图标, 但在同时在按钮上显示图标和文本,QToolButton中文字和图标的排列方式更丰富一些(虽然QPushButton也可以实现,但麻烦得多)。
QToolButton可以使用弹出按钮,弹出一个菜单,以方便提供丰富的操作。
QToolButton的常用方法有:
QToolButton按钮样式:
QToolButton箭头样式:
菜单弹出模式:
QToolButton类继承关系:
创建文件qtoolbutton.py, 使用和QPushButton相似的例子,在Play键添加一个菜单选项,使之可以正常速度,慢速,快速播放动画。完整代码如下:
- import sys
- import os
- from PyQt5 import QtCore, QtGui, QtWidgets
- from PyQt5.QtWidgets import (QApplication, QMainWindow, QLabel, QToolButton, QStyle, QMenu, QAction)
- from PyQt5.QtGui import (QIcon, QMovie)
- from PyQt5.QtCore import Qt
-
- class DemoToolButton(QMainWindow):
- def __init__(self, parent=None):
- super(DemoToolButton, self).__init__(parent)
-
- # 设置窗口标题
- self.setWindowTitle("实战PyQt5: QToolButton Demo!")
- # 设置窗口大小
- self.resize(400, 420)
-
- # 图像尺寸
- img_width = 176 * 2
- img_height = 180 * 2
-
- # 播放状态记录变量
- self.is_playing = False
- self.is_pause = False
- self.total_frame = 0
- self.cur_frame = 0
-
- self.movie = QMovie(os.path.dirname(__file__) + "/use-python.gif")
- self.movie.setScaledSize(QtCore.QSize(img_width, img_height))
- self.movie.setCacheMode(QMovie.CacheAll) #支持回卷
- self.total_frame = self.movie.frameCount() #保存总帧数
- label = QLabel(self)
- label.move(20, 10)
- label.setFixedSize(img_width, img_height)
- label.setMovie(self.movie)
- self.movie.jumpToFrame(0)
-
- self.btn_play = QToolButton(self)
- self.btn_play.setGeometry(20, 380, 60, 24)
- self.btn_play.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)
- self.btn_play.clicked.connect(lambda: self.play(100))
-
- # 创建三种不同速率播放的Action
- action_slow = QAction('慢速播放', self)
- action_slow.triggered.connect(lambda: self.play(25))
- action_normal = QAction('常速播放', self)
- action_normal.triggered.connect(lambda: self.play(100))
- action_fast = QAction('快速播放', self)
- action_fast.triggered.connect(lambda: self.play(400))
-
- #弹出菜单
- self.popup_menu = QMenu(self)
- self.popup_menu.addAction(action_slow)
- self.popup_menu.addAction(action_normal)
- self.popup_menu.addAction(action_fast)
-
- self.btn_play.setPopupMode(QToolButton.MenuButtonPopup)
- self.btn_play.setAutoRaise(True)
- self.btn_play.setMenu(self.popup_menu)
-
- self.setPlayButtonState()
-
- btn_first = QToolButton(self)
- btn_first.setIcon(QApplication.style().standardIcon(QStyle.SP_MediaSkipBackward))
- btn_first.setText("到头")
- btn_first.setToolTip("到第一帧")
- btn_first.setGeometry(90, 380, 60, 24)
- btn_first.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)
- btn_first.setStyleSheet('border: 0px')
- btn_first.clicked.connect(self.firstFrame)
-
- btn_last = QToolButton(self)
- btn_last.setIcon(QApplication.style().standardIcon(QStyle.SP_MediaSkipForward))
- btn_last.setText("到尾")
- btn_last.setToolTip("到最后一帧")
- btn_last.setGeometry(160, 380, 60, 24)
- btn_last.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)
- btn_last.setStyleSheet('border: 0px')
- btn_last.clicked.connect(self.lastFrame)
-
- btn_prev = QToolButton(self)
- btn_prev.setIcon(QApplication.style().standardIcon(QStyle.SP_MediaSeekBackward))
- btn_prev.setText("前帧")
- btn_prev.setToolTip("到前一帧")
- btn_prev.setGeometry(230, 380, 60, 24)
- btn_prev.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)
- btn_prev.setStyleSheet('border: 0px')
- btn_prev.clicked.connect(self.prevFrame)
-
- btn_next = QToolButton(self)
- btn_next.setIcon(QApplication.style().standardIcon(QStyle.SP_MediaSeekForward))
- btn_next.setText("后帧")
- btn_next.setToolTip("到后一帧")
- btn_next.setGeometry(300, 380, 60, 24)
- btn_next.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)
- btn_next.setStyleSheet('border: 0px')
- btn_next.clicked.connect(self.nextFrame)
-
- #修改播放按钮的状态
- def setPlayButtonState(self):
- if self.is_playing :
- self.btn_play.setIcon(QApplication.style().standardIcon(QStyle.SP_MediaStop))
- self.btn_play.setText("停止")
- self.btn_play.setToolTip("点击按钮停止播放")
- self.popup_menu.setEnabled(False)
- else:
- self.btn_play.setIcon(QApplication.style().standardIcon(QStyle.SP_MediaPlay))
- self.btn_play.setText("播放")
- self.btn_play.setToolTip("点击按钮开始播放")
- self.popup_menu.setEnabled(True)
-
- #播放按钮的槽函数
- def play(self, speed):
- if self.is_playing:
- self.movie.stop()
- self.movie.jumpToFrame(0) #回到第一帧
- self.is_playing = False
- else:
- self.movie.start()
- self.movie.setSpeed(speed)
- self.is_playing = True
- self.setPlayButtonState()
-
- #到设定的当前帧
- def toFrame(self):
- if self.is_playing:
- self.movie.stop()
- self.is_playing = False
- self.setPlayButtonState()
- self.movie.jumpToFrame(self.cur_frame)
-
- #到第一帧
- def firstFrame(self):
- self.cur_frame = 0
- self.toFrame()
-
- #到最后一帧
- def lastFrame(self):
- self.cur_frame = self.total_frame - 1
- self.toFrame()
-
- #到前一帧
- def prevFrame(self):
- if self.cur_frame <= 0:
- self.cur_frame = self.total_frame - 1
- else:
- self.cur_frame = self.cur_frame - 1
- self.toFrame()
-
- #到后一帧
- def nextFrame(self):
- if self.cur_frame >= self.total_frame - 1:
- self.cur_frame = 0
- else:
- self.cur_frame = self.cur_frame + 1
- self.toFrame()
-
- if __name__ == '__main__':
- app = QApplication(sys.argv)
- window = DemoToolButton()
- window.show()
- sys.exit(app.exec())
运行结果如下图:
QToolButton测试效果
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。