锋哥原创的PyQt6视频教程:
QPushButton是PyQt6中最常用的控件之一,它被称为按钮控件,允许用户通过单击来执行操作。QPushButton控件既可以显示文本,也可以显示图像,当该控件被单击时,它看起来像是被按下,然后被释放。
QAbstractButton
类属性
-
text
显示文本 -
icon
设置图标 -
iconSize
图标大小 -
shortcut
设置快捷键 -
checkable
设置是否自动切换按钮 -
checked
设置默认选中状态 -
autoRepeat
设置是否会在用户按下时自动重复 -
autoExclusive
设置是否启用自动排他性(设置这个可以变成多选) -
autoRepeatDelay
自动重复的初始延迟(以毫秒为单位) -
autoRepeatInterval
自动重复的时间间隔(以毫秒为单位)
打印
print(QAbstractButton.__subclasses__())
[<class 'PyQt6.QtWidgets.QCheckBox'>, <class 'PyQt6.QtWidgets.QPushButton'>, <class 'PyQt6.QtWidgets.QRadioButton'>, <class 'PyQt6.QtWidgets.QToolButton'>]
所以这四个子类都拥有QAbstractButton的上述属性功能。
QPushButton
类属性
-
autoDefault
将按钮设置为对话框中的默认按钮 -
default
设置按钮的默认状态 -
flat
扁平化
参考代码:
# Form implementation generated from reading ui file '测试2.ui'
#
# Created by: PyQt6 UI code generator 6.4.2
#
# WARNING: Any manual changes made to this file will be lost when pyuic6 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt6 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)
self.pushButton = QtWidgets.QPushButton(parent=Form)
self.pushButton.setGeometry(QtCore.QRect(80, 180, 75, 23))
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("C:/Users/java1234/Desktop/login.png"), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.Off)
self.pushButton.setIcon(icon)
self.pushButton.setCheckable(False)
self.pushButton.setAutoExclusive(False)
self.pushButton.setAutoDefault(True)
self.pushButton.setDefault(False)
self.pushButton.setFlat(False)
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(parent=Form)
self.pushButton_2.setGeometry(QtCore.QRect(210, 180, 75, 23))
icon1 = QtGui.QIcon()
icon1.addPixmap(QtGui.QPixmap("C:/Users/java1234/Desktop/reset.png"), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.Off)
self.pushButton_2.setIcon(icon1)
self.pushButton_2.setIconSize(QtCore.QSize(20, 20))
self.pushButton_2.setAutoDefault(False)
self.pushButton_2.setDefault(True)
self.pushButton_2.setFlat(True)
self.pushButton_2.setObjectName("pushButton_2")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton.setText(_translate("Form", "登录"))
self.pushButton.setShortcut(_translate("Form", "Backspace"))
self.pushButton_2.setText(_translate("Form", "重置"))