Pyqt5系列(三)-基本界面组件之Button(1)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhulove86/article/details/52413767

Button,作为界面中触发动作请求或者命令的一种方式,作为与用户进行的交互操作。PyQt中的Button根据不同的使用场景划分为不同的表现形式。Button的基类QAbstractButton,提供button的通用性功能,此类为抽象类,从因此不能实例化,由其他的Button类继承来实现不同的功能,不同的表现形式。
常见的Button包括,QPushButton,QToolButton,QRadioButton及QCheckBox。这些Button类均继承自QAbstractButton类,根据各自的使用场景通过图形展现出来。


抽象类 QAbstractButton:

QAbstractButton作为抽象类,提供button的通用功能,可按按钮(push button)和可选择按钮(checkable button)。可选择按钮实现有QRadioButton和QCheckBox;可按按钮实现有QPushButton和QToolButton。
任何一种button可以显示带文本(.setText()方法设置文本)和图标(.setIcon()设置图标)的标签。

QAbstractButton 提供的状态:
1、isDown() 提示button是否按下
2、isChecked()提示button是否已经标记
3、isEnable()提示button是否可以被用户点击
4、isCheckAble()提示button是否为可标记
5、setAutoRepeat()设置button是否在用户长按按钮的时候可以自动重复执行。

QAbstractButton 提供的信号:
1、pressed(),当鼠标在button上并点击左键的时候 触发信号
2、released(),当鼠标左键被释放的时候触发信号
3、clicked(),当鼠标首次按下,然后释放,或者快捷键被释放的时候触发信号
4、toggled(),当button的标记状态发生改变的时候触发信号

接下来会针对每一种button进行介绍:


QPushButton :

class QPushButton(QAbstractButton)
 |  QPushButton(QWidget parent=None)
 |  QPushButton(str, QWidget parent=None)
 |  QPushButton(QIcon, str, QWidget parent=None)

由此可见QPushButton继承自QAbstractButton,是一种command按钮。点击执行一些命令,或者响应一些问题。常见的诸如“确认”,“申请”,“取消”,“关闭”,“是”,“否”等按钮。
Command Button经常通过文本来描述执行的动作。有时候我们也会通过快捷键来执行对应按钮的命令。

通过一个示例对QPushButton来进行说明:

#-*- coding:utf-8 -*-
'''
PushButton
'''
__author__ = 'Tony Zhu'

from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import Qt
import sys

class PushButton(QWidget):
    def __init__(self):
        super(PushButton,self).__init__()
        self.initUI()
    def initUI(self):
        self.setWindowTitle("PushButton")
        self.setGeometry(400,400,300,260)

        self.closeButton = QPushButton(self)
        self.closeButton.setText("Close")          #text
        self.closeButton.setIcon(QIcon("close.png")) #icon
        self.closeButton.setShortcut('Ctrl+D')  #shortcut key
        self.closeButton.clicked.connect(self.close)
        self.closeButton.setToolTip("Close the widget") #Tool tip
        self.closeButton.move(100,100)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = PushButton()
    ex.show()
    sys.exit(app.exec_()) 

运行之后的效果:
PushButton

控件说明:

控件类型 控件名称 文本 图标
QPushButton closeButton Close close.png

示例说明:
名称为“Close”的 Buttton,点击该Button之后关闭该窗口。或者通过快捷键“Ctrl+C”的快捷方式亦可关闭该窗口。
代码分析:
其他代码部分可以参考上一篇《Pyqt5系列(二 )-第一个PyQt程序》中的说明。

L21~22:

self.closeButton.setText("Close")          #text
self.closeButton.setIcon(QIcon("close.png")) #icon

setText()方法,设定button的文本
setIcon()方法,设定button的图标
关于button 文本和图标的显示,也可以通过QPushButton的构造函数,在创建对象实例的时候通过参数直接设定。
| QPushButton(str, QWidget parent=None)
| QPushButton(QIcon, str, QWidget parent=None)

L23:

self.closeButton.setShortcut('Ctrl+D')  #shortcut key

给closeButton设定快捷键方式,即通过Ctrl+D实现与点击closeButton一样的功能。

L24:

self.closeButton.clicked.connect(self.close)

closeButton点击事件处理的逻辑:在点击closeButton之后调用QWidget的close()方法。通过connect()方法将点击事件和处理逻辑关联起来 。

L25:

self.closeButton.setToolTip("Close the widget")

setToolTip()设定提示信息,当鼠标移动到button上时显示”Close the widget”提示信息。


QToolButton:

class QToolButton(QAbstractButton)
 |  QToolButton(QWidget parent=None)

同理QToolButton继承自QAbstractButton。QToolButton就是工具操作相关的按钮,通常和QToolBar搭配使用。QToolButton通常不显示文本,而显示图标QIcon。一般QToolButton会在QToolBar::addAction时创建,或者已经存在的action添加到QToolBar时创建。

通过一个示例对QToolButton来进行说明:

#-*- coding:utf-8 -*-
'''
ToolButton
'''
__author__ = 'Tony Zhu'

from PyQt5.QtWidgets import QApplication, QWidget, QToolButton, QMainWindow
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import Qt
import sys

class ToolButton(QMainWindow):
    def __init__(self):
        super(ToolButton,self).__init__()
        self.initUI()
    def initUI(self):
        self.setWindowTitle("ToolButton")
        self.setGeometry(400,400,300,260)

        self.toolbar = self.addToolBar("toolBar")
        self.statusBar()

        self._detailsbutton = QToolButton()                                     
        self._detailsbutton.setCheckable(True)                                  
        self._detailsbutton.setChecked(False)                                   
        self._detailsbutton.setArrowType(Qt.RightArrow)
        self._detailsbutton.setAutoRaise(True)
        #self._detailsbutton.setIcon(QIcon("test.jpg"))
        self._detailsbutton.setToolButtonStyle(Qt.ToolButtonIconOnly)
        self._detailsbutton.clicked.connect(self.showDetail)
        self.toolbar.addWidget(self._detailsbutton)

    def showDetail(self):
        if self._detailsbutton.isChecked():
            self.statusBar().showMessage("Show Detail....")
        else:
            self.statusBar().showMessage("Close Detail....")

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = ToolButton()
    ex.show()
    sys.exit(app.exec_()) 

运行之后的效果:
ToolButton

控件说明:

控件类型 控件名称 文本 图标
QToolButton _detailsbutton 右箭头图标

示例说明:
图标为“右箭头图标”的 Buttton,此按钮有开关之分。当Button打开之后在消息栏显示“Show Detail….”,反之显示“Close Detail”。
代码分析:
其他代码部分可以参考上一篇《Pyqt5系列(二 )-第一个PyQt程序》中的说明。
L24~25:

self._detailsbutton.setCheckable(True)                                         self._detailsbutton.setChecked(False)

setCheckable()方法,“True”设置该button为可选属性,及存在“开”和“关”两种状态。
setChecked()方法,设置button的状态为为选中的状态。

L26:

self._detailsbutton.setArrowType(Qt.RightArrow)

setArrowType()方法,设定button上显示的箭头类型
arrowType,箭头属性,按钮是否显示一个arrow代替正常的icon
Qt.NoArrow 0
Qt.UpArrow 1
Qt.DownArrow 2
Qt.LeftArrow 3
Qt.RightArrow 4

L29:

self._detailsbutton.setToolButtonStyle(Qt.ToolButtonIconOnly)

setToolButtonStyle(),设定button文本和图标显示的样式。程序中的参数为只显示icon不显示文本(Qt.ToolButtonIconOnly)
参数类型如下:
Qt.ToolButtonIconOnly 0 Only display the icon.
Qt.ToolButtonTextOnly 1 Only display the text.
Qt.ToolButtonTextBesideIcon 2 The text appears beside the icon.
Qt.ToolButtonTextUnderIcon 3 The text appears under the icon.
Qt.ToolButtonFollowStyle 4

如果在实际的使用过程中,需要同时显示自定义的icon和文本的时候,可以按照如下参数设置:

self._detailsbutton.setIcon(QIcon("test.jpg"))
self._detailsbutton.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)

对于QPushButton和QToolButton,如上的例子中只是涉及到部分常用的方法,所以对于详细的说明可以通过如下两个途径:

  1. PyQt5 Class Reference 网站:
    http://pyqt.sourceforge.net/Docs/PyQt5/class_reference.html

  2. 在命令行中通过help()方法:
    如 help(QPushButton)

猜你喜欢

转载自blog.csdn.net/zhulove86/article/details/52413767