环境配置
运行环境:anaconda3
框架:pycharm
导包:这里用的中科大的源镜像
pip install PyQt6 -i https://pypi.mirrors.ustc.edu.cn/simple/
pip install pyqt6-tools -i https://pypi.mirrors.ustc.edu.cn/simple/
配置两个拓展工具QTDesigner和PyUIC
QTDesigner:
找到自己的designer的路径,我的是:
D:\anacoda3\Lib\site-packages\qt6_applications\Qt\bin\designer
添加上路径即可
在Tools -> External Tools -> QTDesigner中可以找到,点击如下图所示,就是配置成功
PyUIC:
和上述一样,在setting的external tools中添加PyUIC
program:自己电脑python文件的位置
arguments:-m PyQt6.uic.pyuic $FileName$ -o $FileNameWithoutExtension$.py
working directory:$FileDir$
部署好环境以后运行,可以用下面这个ui代码做测试
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="baseSize">
<size>
<width>400</width>
<height>300</height>
</size>
</property>
<property name="windowTitle">
<string>学python</string>
</property>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>140</x>
<y>80</y>
<width>81</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>python大爷</string>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>
点击ui文件,在tools的external tools中选择刚部署好的PyUIC
可以得到一个ui文件的py版。
即可视为配置成功
文本类控件:
QLabel标签控件
Label控件,又称为标签控件,它主要用于显示用户不能编辑的文本,标识窗体上的对象(例如,给文本框,列表框添加描述信息等),它对应PyQt6中的QLabel类,Label控件本质上是QLabel类 的对象
设置标签文本
对应QLabel对象方法:setText()
设置标签文本的对齐方式
主要用到alignment属性
对齐分两个大维度,分别是水平方向(Horizontal)和垂直方向(Vertical)
水平方向(Horizontal)取值有:
AlignLeft:左对齐
AlignHCenter:水平居中对齐
AlignRight:右对齐
Alignlustify:两端对齐
垂直方向(vertical)取值有:
AlignTop:顶部对齐
AlignVCenter:垂直居中对齐
AlignBottom:底部对齐
参考代码:
self.label.setAlignment(QtCore.Qt.AlignmentFlag.AlignLeading|QtCore.Qt.AlignmentFlag.AlignLeft|QtCore.Qt.AlignmentFlag.AlignVCenter)
setAlignment()方法,枚举值通过QtCore.Qt.AlignmentFlag属性值
设置文本换行显示
假设将标签文本的text值设置为‘QLabel标签控件xxxxxxxxxxx’,在标签宽度不足的情况下,系统会默认只显示部分文字
遇到这种情况,可以设置标签中的文本换行显示,只需要在Qt Designer设计器的属性编辑器中,将worldWrap属性后面的复选框选中即可
预览效果:
用到setWordWrap()方法
self.label.setWordWrap(True)
为标签设置超链接
为Lable标签设置超链接时,可以直接在QLabel类的setText()方法中使用HTML中的<a>标签设置超链接文本,然后将Label标签的setOpenExternalLinks()设置为True,以便允许访问超链接
self.label.setOpenExternalLinks(True)
为标签设置图片
为Label标签设置图片时,需要使用QLabel类的setPixmap()方法,该方法中需要有一个QPixmap对象,表示图标对象
self.label.setPixmap(QtGui.QPixmap("C:/Users/G/Pictures/Default.jpg"))
获取标签文本
获取Label标签中的文本需要使用QLabel类的text()方法
import sys
from PyQt6.QtWidgets import QApplication, QLabel
from PyQt6 import uic
if __name__ == '__main__':
app = QApplication(sys.argv)
ui = uic.loadUi('./Qlabel标签控件.ui')
myLabel: QLabel = ui.label
print(myLabel.text())
ui.show()
sys.exit(app.exec())
补充知识点-加载ui文件
加载ui文件可以使ui文件在python解释器上运行,而不用通过QT Designer
具体代码如下:
from PyQt6.QtWidgets import QApplication
from PyQt6 import uic
import sys
if __name__ == '__main__':
app = QApplication(sys.argv)
ui = uic.loadUi('./ui文件.ui') # 同级别目录下的ui文件
ui.show()
sys.exit(app.exec())
ui文件:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="baseSize">
<size>
<width>400</width>
<height>300</height>
</size>
</property>
<property name="windowTitle">
<string>学python</string>
</property>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>140</x>
<y>80</y>
<width>81</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>用户名:</string>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>
运行效果:
QLineEdit单行文本框
QLineEdit是单行文本框,该控件只能输入单行字符串。该类的常用方法如下:
setText():设置文本框内容
text():获取文本框内容
setPlaceholderText():设置文本框浮现文字
setMaxLength():设置允许文本框内输入字符的最大长度
setAlignmer():设置文本对齐方式
setReadOnly():设置文本框只读
setFocus():使文本框得到焦点
setEchoModel():设置文本框显示字符的模式,有以下4种模式
QLineEdit.Normal:正常显示输入的字符,这是默认设置
QLineEdit.NoEcho:不显示任何输入的字符(不是不输入,只是不显示)
QLineEdit.Password:显示与平台相关的密码掩码字符,而不是实际输入的字符
QLineEdit.PasswordEchoOnEdit:在编辑时显示字符,失去焦点后显示密码掩码字符
setValidator():设置文本框验证器,有以下3种模式
QIntValidator:限制输入整数
QDoubleValidator:限制输入小数
QRegExpValidator:检查输入是否符合设置的正则表达式
setInputMask():设置掩码,掩码通常由掩码字符和分隔符组成,后面可以跟一个分号和空白字符,空白字符在编辑完成后会从文本框中删除,常用的掩码有以下几种形式:
日期掩码:0000-00-00
时间掩码:00:00:00
序列号掩码:>AAAAA.AAAAA.AAAAA.AAAAA.AAAAA;#
可以自己布局再结合我的代码跑一下,
QLineEdit单行文本框加载ui文件的python代码
from PyQt6.QtGui import QValidator, QIntValidator, QDoubleValidator
from PyQt6.QtWidgets import QApplication, QLineEdit
from PyQt6 import uic
import sys
if __name__ == '__main__':
app = QApplication(sys.argv)
ui = uic.loadUi('./QLineEdit单行文本框.ui')
myLineEdit1: QLineEdit = ui.lineEdit
myLineEdit2: QLineEdit = ui.lineEdit_2
myLineEdit3: QLineEdit = ui.lineEdit_3
myLineEdit4: QLineEdit = ui.lineEdit_4
myLineEdit5: QLineEdit = ui.lineEdit_5
myLineEdit5.setFocus()
myLineEdit6: QLineEdit = ui.lineEdit_6
myLineEdit7: QLineEdit = ui.lineEdit_7
myLineEdit8: QLineEdit = ui.lineEdit_8
myLineEdit9: QLineEdit = ui.lineEdit_9
# print(QValidator.__subclasses__()) # 可以检查QValidator的子类
myLineEdit9.setValidator(QIntValidator())
myLineEdit10: QLineEdit = ui.lineEdit_10
myLineEdit10.setValidator(QDoubleValidator())
myLineEdit11: QLineEdit = ui.lineEdit_11
myLineEdit12: QLineEdit = ui.lineEdit_12
myLineEdit13: QLineEdit = ui.lineEdit_13
# print(myLineEdit.text())
# myLineEdit.clear()
ui.show()
sys.exit(app.exec())
QLineEdit单行文本框python代码:
# Form implementation generated from reading ui file 'QLineEdit单行文本框.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(800, 600)
self.lineEdit = QtWidgets.QLineEdit(parent=Form)
self.lineEdit.setGeometry(QtCore.QRect(20, 20, 113, 31))
self.lineEdit.setInputMask("")
self.lineEdit.setMaxLength(5)
self.lineEdit.setAlignment(QtCore.Qt.AlignmentFlag.AlignLeading|QtCore.Qt.AlignmentFlag.AlignLeft|QtCore.Qt.AlignmentFlag.AlignVCenter)
self.lineEdit.setReadOnly(False)
self.lineEdit.setPlaceholderText("")
self.lineEdit.setCursorMoveStyle(QtCore.Qt.CursorMoveStyle.LogicalMoveStyle)
self.lineEdit.setClearButtonEnabled(False)
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(parent=Form)
self.lineEdit_2.setGeometry(QtCore.QRect(20, 60, 151, 31))
self.lineEdit_2.setText("")
self.lineEdit_2.setMaxLength(5)
self.lineEdit_2.setReadOnly(False)
self.lineEdit_2.setObjectName("lineEdit_2")
self.lineEdit_3 = QtWidgets.QLineEdit(parent=Form)
self.lineEdit_3.setGeometry(QtCore.QRect(20, 110, 221, 31))
self.lineEdit_3.setMaxLength(3)
self.lineEdit_3.setObjectName("lineEdit_3")
self.lineEdit_4 = QtWidgets.QLineEdit(parent=Form)
self.lineEdit_4.setGeometry(QtCore.QRect(20, 160, 181, 21))
self.lineEdit_4.setReadOnly(True)
self.lineEdit_4.setObjectName("lineEdit_4")
self.lineEdit_6 = QtWidgets.QLineEdit(parent=Form)
self.lineEdit_6.setGeometry(QtCore.QRect(20, 220, 301, 20))
self.lineEdit_6.setEchoMode(QtWidgets.QLineEdit.EchoMode.NoEcho)
self.lineEdit_6.setObjectName("lineEdit_6")
self.lineEdit_7 = QtWidgets.QLineEdit(parent=Form)
self.lineEdit_7.setGeometry(QtCore.QRect(20, 250, 341, 20))
self.lineEdit_7.setEchoMode(QtWidgets.QLineEdit.EchoMode.Password)
self.lineEdit_7.setObjectName("lineEdit_7")
self.lineEdit_8 = QtWidgets.QLineEdit(parent=Form)
self.lineEdit_8.setGeometry(QtCore.QRect(20, 290, 311, 20))
self.lineEdit_8.setEchoMode(QtWidgets.QLineEdit.EchoMode.PasswordEchoOnEdit)
self.lineEdit_8.setObjectName("lineEdit_8")
self.lineEdit_9 = QtWidgets.QLineEdit(parent=Form)
self.lineEdit_9.setGeometry(QtCore.QRect(20, 320, 113, 20))
self.lineEdit_9.setObjectName("lineEdit_9")
self.lineEdit_10 = QtWidgets.QLineEdit(parent=Form)
self.lineEdit_10.setGeometry(QtCore.QRect(20, 360, 113, 20))
self.lineEdit_10.setObjectName("lineEdit_10")
self.lineEdit_11 = QtWidgets.QLineEdit(parent=Form)
self.lineEdit_11.setGeometry(QtCore.QRect(20, 400, 141, 20))
self.lineEdit_11.setObjectName("lineEdit_11")
self.lineEdit_12 = QtWidgets.QLineEdit(parent=Form)
self.lineEdit_12.setGeometry(QtCore.QRect(20, 450, 113, 20))
self.lineEdit_12.setObjectName("lineEdit_12")
self.lineEdit_13 = QtWidgets.QLineEdit(parent=Form)
self.lineEdit_13.setGeometry(QtCore.QRect(20, 490, 113, 20))
self.lineEdit_13.setObjectName("lineEdit_13")
self.lineEdit_5 = QtWidgets.QLineEdit(parent=Form)
self.lineEdit_5.setGeometry(QtCore.QRect(20, 190, 113, 20))
self.lineEdit_5.setObjectName("lineEdit_5")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.lineEdit.setText(_translate("Form", "获取文本框"))
self.lineEdit_2.setPlaceholderText(_translate("Form", "设置文本框浮现文字"))
self.lineEdit_3.setPlaceholderText(_translate("Form", "文本框内输入字符的最大长度,这里是3"))
self.lineEdit_4.setPlaceholderText(_translate("Form", "设置文本框只读"))
self.lineEdit_6.setPlaceholderText(_translate("Form", "不显示任何输入的字符(不是不输入,只是不显示)"))
self.lineEdit_7.setPlaceholderText(_translate("Form", "显示与平台相关的密码掩码字符,而不是实际输入的字符"))
self.lineEdit_8.setPlaceholderText(_translate("Form", "在编辑时显示字符,失去焦点后显示密码掩码字符"))
self.lineEdit_9.setPlaceholderText(_translate("Form", "限制输入整数"))
self.lineEdit_10.setPlaceholderText(_translate("Form", "限制输入小数"))
self.lineEdit_11.setPlaceholderText(_translate("Form", "日期掩码"))
self.lineEdit_12.setPlaceholderText(_translate("Form", "时间掩码"))
self.lineEdit_13.setPlaceholderText(_translate("Form", "序列号掩码"))
self.lineEdit_5.setPlaceholderText(_translate("Form", "焦点"))
QTextEdit多行富文本框控件
QTextEdit是多行文本框控件,主要用来显示多行文本的内容,当文本内容提出控件的显示范围时,该控件将显示垂直滚动条;另外,TextEdit控件不仅可以显示纯文本内容,还支持显示HTML网页。
QTextEdit类的常用方法如下:
setPlainText():设置文本内容
toPlainText():获取文本内容
setTextColor():设置文本颜色,例如,将文本设置为红色,可以将该方法的参数设置为QtGui.QColor(255.0.0)
setHTML():设置HTML文档内容
toHTML():获取HTML文档内容
setLineWrapMode():
lineWrapMode:lineWrapMode属性用于控制换行模式,其类型为枚举类型
QTextEdit.LineWrapMode,缺省值为WidgetWidth,表示以词为单位在编辑
器右边换行,换行出现在空白处,保持整个单词的完整性,可以调用方法
lineWrapMode()、setLineWrapMode()来访问该属性,如果设置换行模式为
FixedPixelWidth(距离控件左侧的像素距离)或FixedColumnWidth(距离控
件左侧的列距离)同时需要调用setLineWrapColumnOrWidth()方法设置换行
的像素宽度或字符宽度,这两种模式不会保持单词的完整性
clear():清除所有内容
overwriterMode():
overwriterMode属性用于控制用户输入文本是否替换现有文本,如果为True,
则输入字符从当前光标位置开始逐一替换当前的字符,为Flase则在光标处插
入输入字符,缺省值为Flase,可以通过方法overwriterMode()、setOverwriterMode()
进行访问
按照我的布局格式并结合python代码,将每个功能敲一遍
QT布局:
QTextEdit多行富文本框加载ui文件的python代码:
import sys
from PyQt6.QtWidgets import QApplication, QTextEdit
from PyQt6 import uic, QtGui
if __name__ == '__main__':
app = QApplication(sys.argv)
ui = uic.loadUi('./QTextEdit多行富文本框控件.ui')
myTextEdit_1: QTextEdit = ui.textEdit
myTextEdit_2: QTextEdit = ui.textEdit_2
myTextEdit_1.setTextColor(QtGui.QColor(255, 0, 0))
myTextEdit_1.setTextBackgroundColor(QtGui.QColor(255, 255, 0))
myTextEdit_1.setPlainText('<thead><tr><th>ID</th><th>姓名</th><th>年龄</th></tr></thead>')
myTextEdit_2.setHtml('<thead><tr><th>ID</th><th>姓名</th><th>年龄</th></tr></thead><a href="https://blog.csdn.net/2301_80851925?type=blog">csdn博主主页</a>')
print(myTextEdit_1.toPlainText()) # 获取文本内容
print(myTextEdit_2.toHtml()) # 获取HTML文档内容
# myTextEdit_1.clear() # 清空文本
# myTextEdit_2.clear()
ui.show()
sys.exit(app.exec())
QTextEdit多行富文本框python代码
# Form implementation generated from reading ui file 'QTextEdit多行富文本框控件.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(716, 643)
self.textEdit = QtWidgets.QTextEdit(parent=Form)
self.textEdit.setGeometry(QtCore.QRect(30, 50, 151, 171))
self.textEdit.setUndoRedoEnabled(True)
self.textEdit.setLineWrapMode(QtWidgets.QTextEdit.LineWrapMode.NoWrap)
self.textEdit.setObjectName("textEdit")
self.textEdit_2 = QtWidgets.QTextEdit(parent=Form)
self.textEdit_2.setGeometry(QtCore.QRect(200, 50, 161, 171))
self.textEdit_2.setObjectName("textEdit_2")
self.label = QtWidgets.QLabel(parent=Form)
self.label.setGeometry(QtCore.QRect(80, 230, 54, 16))
self.label.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(parent=Form)
self.label_2.setGeometry(QtCore.QRect(240, 230, 71, 16))
self.label_2.setTextFormat(QtCore.Qt.TextFormat.AutoText)
self.label_2.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
self.label_2.setObjectName("label_2")
self.textEdit_3 = QtWidgets.QTextEdit(parent=Form)
self.textEdit_3.setGeometry(QtCore.QRect(30, 300, 104, 71))
self.textEdit_3.setLineWrapMode(QtWidgets.QTextEdit.LineWrapMode.NoWrap)
self.textEdit_3.setObjectName("textEdit_3")
self.textEdit_4 = QtWidgets.QTextEdit(parent=Form)
self.textEdit_4.setGeometry(QtCore.QRect(180, 300, 104, 71))
self.textEdit_4.setObjectName("textEdit_4")
self.textEdit_5 = QtWidgets.QTextEdit(parent=Form)
self.textEdit_5.setGeometry(QtCore.QRect(340, 300, 104, 71))
self.textEdit_5.setLineWrapMode(QtWidgets.QTextEdit.LineWrapMode.FixedPixelWidth)
self.textEdit_5.setLineWrapColumnOrWidth(100)
self.textEdit_5.setObjectName("textEdit_5")
self.textEdit_6 = QtWidgets.QTextEdit(parent=Form)
self.textEdit_6.setGeometry(QtCore.QRect(490, 300, 104, 71))
self.textEdit_6.setLineWrapMode(QtWidgets.QTextEdit.LineWrapMode.FixedColumnWidth)
self.textEdit_6.setLineWrapColumnOrWidth(5)
self.textEdit_6.setObjectName("textEdit_6")
self.label_3 = QtWidgets.QLabel(parent=Form)
self.label_3.setGeometry(QtCore.QRect(40, 380, 81, 16))
self.label_3.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
self.label_3.setObjectName("label_3")
self.label_4 = QtWidgets.QLabel(parent=Form)
self.label_4.setGeometry(QtCore.QRect(180, 380, 111, 16))
self.label_4.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
self.label_4.setObjectName("label_4")
self.label_5 = QtWidgets.QLabel(parent=Form)
self.label_5.setGeometry(QtCore.QRect(340, 380, 111, 16))
self.label_5.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
self.label_5.setObjectName("label_5")
self.label_6 = QtWidgets.QLabel(parent=Form)
self.label_6.setGeometry(QtCore.QRect(490, 380, 111, 16))
self.label_6.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
self.label_6.setObjectName("label_6")
self.textEdit_7 = QtWidgets.QTextEdit(parent=Form)
self.textEdit_7.setGeometry(QtCore.QRect(30, 430, 104, 71))
self.textEdit_7.setLineWrapMode(QtWidgets.QTextEdit.LineWrapMode.FixedColumnWidth)
self.textEdit_7.setLineWrapColumnOrWidth(5)
self.textEdit_7.setOverwriteMode(True)
self.textEdit_7.setAcceptRichText(True)
self.textEdit_7.setObjectName("textEdit_7")
self.label_7 = QtWidgets.QLabel(parent=Form)
self.label_7.setGeometry(QtCore.QRect(30, 510, 101, 71))
self.label_7.setAlignment(QtCore.Qt.AlignmentFlag.AlignLeading|QtCore.Qt.AlignmentFlag.AlignLeft|QtCore.Qt.AlignmentFlag.AlignTop)
self.label_7.setWordWrap(True)
self.label_7.setObjectName("label_7")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.label.setText(_translate("Form", "纯文本"))
self.label_2.setText(_translate("Form", "HTML网页"))
self.label_3.setText(_translate("Form", "不换行"))
self.label_4.setText(_translate("Form", "以边界为换行的"))
self.label_5.setText(_translate("Form", "以像素为换行标准"))
self.label_6.setText(_translate("Form", "以列数为换行标准"))
self.label_7.setText(_translate("Form", "overwriterMode:替换文本"))
QPlainTextEdit纯文本控件
QPlainTextEdit纯文本控件,主要用来显示多行的文本内容
QPlainTextEdit类的常用方法如下:
这个比较简单,也很常用,内容和上面的差不多,属性可以根据我这个进行修改,再看看源代码
QPlainTextEdit纯文本控件加载ui文件的python代码:
import sys
from PyQt6.QtWidgets import QApplication, QPlainTextEdit
from PyQt6 import uic
if __name__ == '__main__':
app = QApplication(sys.argv)
ui = uic.loadUi('./QPlainTextEdit纯文本控件.ui')
myPlainTextEdit: QPlainTextEdit = ui.plainTextEdit
ui.show()
sys.exit(app.exec())
QPlainTextEdit纯文本控件Python代码
# Form implementation generated from reading ui file 'QPlainTextEdit纯文本控件.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.plainTextEdit = QtWidgets.QPlainTextEdit(parent=Form)
self.plainTextEdit.setGeometry(QtCore.QRect(110, 70, 104, 71))
self.plainTextEdit.setReadOnly(True)
self.plainTextEdit.setOverwriteMode(True)
self.plainTextEdit.setBackgroundVisible(True)
self.plainTextEdit.setObjectName("plainTextEdit")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
QSpinBox整数数字选择控件
QSpinBox是一个整数数字选择控件,该控件提供一对上下箭头,用户可以单击上下箭头选择数值,也可以直接输入,如果输入的数值大于设置的最大值,或者小于设置的最小值,SpinBox将不会接受输入
SpinBox类的常用方法如下:
setValue():设置控件的当前值
setMaximum():设置最大值
setMinimum():设置最小值
setRange():设置取值范围(包括最大值和最小值)
setSingleStet():单击上下箭头时的步长值
value():获取控件中的值
setStepType():
QAbstractSpinBox::DefaultStepType:固定步长,可以通过setSingleStep()函数设置
QAbstractSpinBox::AdaptiveDecimalStepType:自适应步长,根据数字的大小自动调整步长
setPrefix():设置前缀
setSuffix():设置后缀
Qt布局:
QSpinBox整数数字选择控件加载ui文件的python代码:
import sys
from PyQt6.QtWidgets import QApplication,QSpinBox
from PyQt6 import uic
if __name__ == '__main__':
app = QApplication(sys.argv)
ui = uic.loadUi('./QSpinBox整数数字选择控件.ui')
mySpinBox_1: QSpinBox = ui.spinBox
mySpinBox_2: QSpinBox = ui.spinBox_2
ui.show()
sys.exit(app.exec())
QSpinBox整数数字选择控件Python代码
# Form implementation generated from reading ui file 'QSpinBox整数数字选择控件.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.spinBox = QtWidgets.QSpinBox(parent=Form)
self.spinBox.setGeometry(QtCore.QRect(30, 30, 61, 22))
self.spinBox.setMinimum(-2)
self.spinBox.setMaximum(10)
self.spinBox.setSingleStep(2)
self.spinBox.setStepType(QtWidgets.QAbstractSpinBox.StepType.DefaultStepType)
self.spinBox.setObjectName("spinBox")
self.spinBox_2 = QtWidgets.QSpinBox(parent=Form)
self.spinBox_2.setGeometry(QtCore.QRect(130, 30, 61, 22))
self.spinBox_2.setMinimum(-2)
self.spinBox_2.setMaximum(100)
self.spinBox_2.setStepType(QtWidgets.QAbstractSpinBox.StepType.AdaptiveDecimalStepType)
self.spinBox_2.setObjectName("spinBox_2")
self.label = QtWidgets.QLabel(parent=Form)
self.label.setGeometry(QtCore.QRect(30, 50, 71, 71))
self.label.setWordWrap(True)
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(parent=Form)
self.label_2.setGeometry(QtCore.QRect(130, 50, 91, 61))
self.label_2.setWordWrap(True)
self.label_2.setObjectName("label_2")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.spinBox.setSuffix(_translate("Form", "件"))
self.spinBox.setPrefix(_translate("Form", "第"))
self.spinBox_2.setSuffix(_translate("Form", "件"))
self.spinBox_2.setPrefix(_translate("Form", "第"))
self.label.setText(_translate("Form", "固定步长,最大值10,最小值-2"))
self.label_2.setText(_translate("Form", "自动调整步长,最大值100,最小值-2"))
QDoubleSpinBox小数数字选择控件
QDoubleSpinBox与QSpinBox控件类似,区别是,它用来选择小数数字,并且默认保留两位小数
QDoubleSpinBox控件的使用方法与QSpinBox类似,但由于它处理的是小数数字,因此,该控件提供了一个setDecimals()方法,用来设置小数的位数
QDoubleSpinBox小数数字选择控件加载ui文件的python代码:
import sys
from PyQt6.QtWidgets import QApplication, QDoubleSpinBox
from PyQt6 import uic
if __name__ == '__main__':
app = QApplication(sys.argv)
ui = uic.loadUi('./QDoubleSpinBox小数数字选择控件.ui')
myDoubleSpinBox: QDoubleSpinBox = ui.doubleSpinBox
ui.show()
sys.exit(app.exec())
QDoubleSpinBox小数数字选择控件Python代码
# Form implementation generated from reading ui file 'QDoubleSpinBox小数数字选择控件.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.doubleSpinBox = QtWidgets.QDoubleSpinBox(parent=Form)
self.doubleSpinBox.setGeometry(QtCore.QRect(130, 80, 91, 22))
self.doubleSpinBox.setDecimals(3)
self.doubleSpinBox.setObjectName("doubleSpinBox")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
QLCDNumber液晶数字显示控件
QLCDNumber控件主要用来显示液晶数字
QLCDNumber类的常用方法如下:
setDigitCount():设置可以显示的数字数量
setSmallDecimalPoint():是否有小数点,如果有小数点,就可以显示小数
setMode():设置显示数字的模式,有四种模式:bin(二进制)、Oct(八进制)、Dec(十进制)、Hex(十六进制)
setSegmentStyle():设置显示样式有3种样式Outline、Filed和Flat
value():获取显示的数值
QLCDNumber液晶数字显示控件加载ui文件的python代码
import sys
from PyQt6.QtWidgets import QApplication, QLCDNumber
from PyQt6 import uic
if __name__ == '__main__':
app = QApplication(sys.argv)
ui = uic.loadUi('./QLCDNumber液晶数字显示控件.ui')
myQLCDNumber_1: QLCDNumber = ui.lcdNumber
myQLCDNumber_2: QLCDNumber = ui.lcdNumber_2
myQLCDNumber_3: QLCDNumber = ui.lcdNumber_3
myQLCDNumber_4: QLCDNumber = ui.lcdNumber_4
myQLCDNumber_5: QLCDNumber = ui.lcdNumber_5
myQLCDNumber_6: QLCDNumber = ui.lcdNumber_6
myQLCDNumber_7: QLCDNumber = ui.lcdNumber_7
myQLCDNumber_8: QLCDNumber = ui.lcdNumber_8
ui.show()
sys.exit(app.exec())
QLCDNumber液晶数字显示控件的python代码
# Form implementation generated from reading ui file 'QLCDNumber液晶数字显示控件.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(746, 683)
self.lcdNumber = QtWidgets.QLCDNumber(parent=Form)
self.lcdNumber.setGeometry(QtCore.QRect(50, 20, 151, 23))
self.lcdNumber.setSmallDecimalPoint(False)
self.lcdNumber.setDigitCount(2)
self.lcdNumber.setMode(QtWidgets.QLCDNumber.Mode.Dec)
self.lcdNumber.setSegmentStyle(QtWidgets.QLCDNumber.SegmentStyle.Flat)
self.lcdNumber.setProperty("value", 33.0)
self.lcdNumber.setProperty("intValue", 33)
self.lcdNumber.setObjectName("lcdNumber")
self.label = QtWidgets.QLabel(parent=Form)
self.label.setGeometry(QtCore.QRect(10, 40, 241, 61))
self.label.setObjectName("label")
self.lcdNumber_2 = QtWidgets.QLCDNumber(parent=Form)
self.lcdNumber_2.setGeometry(QtCore.QRect(400, 20, 201, 23))
self.lcdNumber_2.setSmallDecimalPoint(True)
self.lcdNumber_2.setProperty("value", 3.14)
self.lcdNumber_2.setObjectName("lcdNumber_2")
self.label_2 = QtWidgets.QLabel(parent=Form)
self.label_2.setGeometry(QtCore.QRect(320, 70, 421, 20))
self.label_2.setObjectName("label_2")
self.lcdNumber_3 = QtWidgets.QLCDNumber(parent=Form)
self.lcdNumber_3.setGeometry(QtCore.QRect(30, 140, 201, 23))
self.lcdNumber_3.setMode(QtWidgets.QLCDNumber.Mode.Bin)
self.lcdNumber_3.setProperty("value", 6.0)
self.lcdNumber_3.setObjectName("lcdNumber_3")
self.label_3 = QtWidgets.QLabel(parent=Form)
self.label_3.setGeometry(QtCore.QRect(80, 180, 131, 16))
self.label_3.setObjectName("label_3")
self.lcdNumber_4 = QtWidgets.QLCDNumber(parent=Form)
self.lcdNumber_4.setGeometry(QtCore.QRect(420, 140, 241, 23))
self.lcdNumber_4.setMode(QtWidgets.QLCDNumber.Mode.Oct)
self.lcdNumber_4.setProperty("value", 16.0)
self.lcdNumber_4.setObjectName("lcdNumber_4")
self.label_4 = QtWidgets.QLabel(parent=Form)
self.label_4.setGeometry(QtCore.QRect(490, 180, 101, 16))
self.label_4.setObjectName("label_4")
self.lcdNumber_5 = QtWidgets.QLCDNumber(parent=Form)
self.lcdNumber_5.setGeometry(QtCore.QRect(40, 270, 201, 23))
self.lcdNumber_5.setSmallDecimalPoint(True)
self.lcdNumber_5.setMode(QtWidgets.QLCDNumber.Mode.Hex)
self.lcdNumber_5.setProperty("value", 64.0)
self.lcdNumber_5.setObjectName("lcdNumber_5")
self.label_5 = QtWidgets.QLabel(parent=Form)
self.label_5.setGeometry(QtCore.QRect(90, 300, 121, 16))
self.label_5.setObjectName("label_5")
self.lcdNumber_6 = QtWidgets.QLCDNumber(parent=Form)
self.lcdNumber_6.setGeometry(QtCore.QRect(50, 410, 91, 23))
self.lcdNumber_6.setSegmentStyle(QtWidgets.QLCDNumber.SegmentStyle.Outline)
self.lcdNumber_6.setObjectName("lcdNumber_6")
self.label_6 = QtWidgets.QLabel(parent=Form)
self.label_6.setGeometry(QtCore.QRect(60, 370, 411, 16))
self.label_6.setObjectName("label_6")
self.label_7 = QtWidgets.QLabel(parent=Form)
self.label_7.setGeometry(QtCore.QRect(70, 460, 54, 16))
self.label_7.setObjectName("label_7")
self.lcdNumber_7 = QtWidgets.QLCDNumber(parent=Form)
self.lcdNumber_7.setGeometry(QtCore.QRect(200, 410, 81, 23))
self.lcdNumber_7.setObjectName("lcdNumber_7")
self.label_8 = QtWidgets.QLabel(parent=Form)
self.label_8.setGeometry(QtCore.QRect(200, 460, 54, 16))
self.label_8.setObjectName("label_8")
self.lcdNumber_8 = QtWidgets.QLCDNumber(parent=Form)
self.lcdNumber_8.setGeometry(QtCore.QRect(350, 410, 71, 23))
self.lcdNumber_8.setSegmentStyle(QtWidgets.QLCDNumber.SegmentStyle.Flat)
self.lcdNumber_8.setObjectName("lcdNumber_8")
self.label_9 = QtWidgets.QLabel(parent=Form)
self.label_9.setGeometry(QtCore.QRect(370, 450, 54, 16))
self.label_9.setObjectName("label_9")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.label.setText(_translate("Form", "setDigitCount():设置可以显示的数字数量"))
self.label_2.setText(_translate("Form", "setSmallDecimalPoint():是否有小数点,如果有小数点,就可以显示小数"))
self.label_3.setText(_translate("Form", "bin(二进制)"))
self.label_4.setText(_translate("Form", "Oct(八进制)"))
self.label_5.setText(_translate("Form", "Hex(十六进制)"))
self.label_6.setText(_translate("Form", "setSegmentStyle():设置显示样式有3种样式Outline、Filed和Flat"))
self.label_7.setText(_translate("Form", "Outline"))
self.label_8.setText(_translate("Form", "Filed"))
self.label_9.setText(_translate("Form", "Flat"))
按钮类控件
按钮类控件主要用来执行一些命令操作,PyQt6中的按钮类控件主要有QPushButton、QToolButton、QCommandLinkButton、QRadioButton、QCheckButton、QCheckBox和QDialogButtonBox
QAbstractButton类属性
text-显示文本
icon-设置图标
iconSize-图标大小
shortcut-设置快捷键
checkable-设置是否自动切换按钮
checked-设置默认选中状态
autoRepeat-设置是否会在用户按下时自动重复
autoExclusive-设置是否启用自动排他性(设置这个可以变成多选)
autoRepeatDelay-自动重复的初始延迟(以毫秒为单位)
autoRepeatInterval-自动重复的时间间隔(以毫秒为单位)
查看QAbstractButton的子类
print(QAbstractButton.__subclasses__())
[<class 'PyQt6.QtWidgets.QCheckBox'>, <class 'PyQt6.QtWidgets.QPushButton'>, <class 'PyQt6.QtWidgets.QRadioButton'>, <class 'PyQt6.QtWidgets.QToolButton'>]
QPushButton按钮控件
QPushButton是PyQt6中最常用的控件之一,它被称为按钮控件,允许用户通过单击来执行操作,QPushButton控件既可以显示文本,也可以显示图像,当该控件被单击时,它看起来像是被按下,然后被释放
QPushButton类属性
autoDefault-将按钮设置为对话框中的默认按钮-第一次都一样,选过一次后,光标在其边框上
default-设置按钮的默认状态-第一次光标就在边框上
flat-扁平化-去框留字
Qt布局
QPushButton按钮控件的python代码
# Form implementation generated from reading ui file 'QPushButton按钮控件.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(70, 100, 75, 24))
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("C:/Users/G/Desktop/login.png"), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.Off)
self.pushButton.setIcon(icon)
self.pushButton.setAutoDefault(False)
self.pushButton.setDefault(False)
self.pushButton.setFlat(True)
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(parent=Form)
self.pushButton_2.setGeometry(QtCore.QRect(190, 100, 75, 24))
icon1 = QtGui.QIcon()
icon1.addPixmap(QtGui.QPixmap("C:/Users/G/Desktop/reset.png"), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.Off)
self.pushButton_2.setIcon(icon1)
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_2.setText(_translate("Form", "重置"))
QpushButton按钮控件加载ui文件的Python代码
import sys
from PyQt6.QtWidgets import QApplication, QPushButton, QAbstractButton
from PyQt6 import uic
if __name__ == '__main__':
app = QApplication(sys.argv)
ui = uic.loadUi('./QPushButton按钮控件.ui')
myQPushButton_1: QPushButton = ui.pushButton
myQPushButton_2: QPushButton = ui.pushButton
print(QAbstractButton.__subclasses__())
ui.show()
sys.exit(app.exec())
QToolButton工具按钮控件
QToolButton控件是一个工具按钮,它本质上是一个按钮,只是在按钮中提供了默认文本‘...’和可选的箭头类型
QToolButton控件的使用方法与QPushButton类似:不同的是,QToolButton控件可以设置工具按钮的显示样式和箭头类型,其中,工具按钮的显示样式通过QToolButton类的setToolButtonStyle()方法进行设置
工具按钮的箭头类型通过QToolButton类的setArrowType()方法进行设置
1.工具按钮初了解
setText():设置按钮提示文本
setIcon(QIcon()):设置按钮图标
setIconSize(QSize):设置图标大小
setToolTip(str):因为图标和提示文本同时出现时,只显示图标,通过这个可以设置不一样的提示文本
2.样式设置
setToolButtonStyle(Qt.ToolButtonStyle)
Qt.ToolButtonStyle的风格有以下取值:
Qt.ToolButtonIconOnly:仅显示图标
Qt.ToolButtonTextOnly:仅显示文字
Qt.ToolButtonTextBesideIcon:文本显示在图标旁边
Qt.ToolButtonTextUnderIcon:文本显示在图标下方
Qt.ToolButtonFollowStyle:遵循风格
3.箭头样式
setArrowType(Qt.ArrowType)
Qt.ArrowType的风格如下:
Qt.NoArrow:无箭头
Qt.UpArrow:向上箭头
Qt.DownArrow:向下箭头
Qt.LeftArrow:向左箭头
Qt.RightArrow:向右箭头
4.自动提升
setAutoRalse(Bool):True:自动提升
5.菜单及弹出方式
setPopupMode(QToolButton.ToolButtonPopupMode)
QToolButton.ToolButtonPopupMode的风格如下:
QToolButton.DelayedPopup:鼠标按住一会儿才显示菜单
QToolButton.MenuButtonPopup:有一个专门提示的提示箭头
QToolButton.InstantPopup:点了按钮就显示菜单
根据上面的各种方式,逐一进行练习
QToolButton按钮控件的python代码
# Form implementation generated from reading ui file 'QToolButton按钮控件.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)
Form.setToolTip("")
self.toolButton = QtWidgets.QToolButton(parent=Form)
self.toolButton.setGeometry(QtCore.QRect(10, 20, 91, 51))
self.toolButton.setToolTip("")
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("C:/Users/G/Desktop/menu.png"), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.Off)
self.toolButton.setIcon(icon)
self.toolButton.setPopupMode(QtWidgets.QToolButton.ToolButtonPopupMode.DelayedPopup)
self.toolButton.setToolButtonStyle(QtCore.Qt.ToolButtonStyle.ToolButtonTextBesideIcon)
self.toolButton.setAutoRaise(True)
self.toolButton.setArrowType(QtCore.Qt.ArrowType.NoArrow)
self.toolButton.setObjectName("toolButton")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.toolButton.setText(_translate("Form", "菜单"))
self.toolButton.setShortcut(_translate("Form", "Enter"))
QToolButton按钮控件加载ui文件的python代码
import sys
from PyQt6.QtWidgets import QApplication, QToolButton
from PyQt6 import uic
if __name__ == '__main__':
app = QApplication(sys.argv)
ui = uic.loadUi('./QToolButton按钮控件.ui')
myQToolButton: QToolButton = ui.toolButton
ui.show()
sys.exit(app.exec())
QCommandLinkButton命令链接按钮控件
CommandLinkButton对应类为QCommandLinkButton,实际上是从pushButton继承过来的一种按钮,外观像是一个被设置了扁平化的QPushButton,与PushButton不同主要如下:
.可以在按钮上显示双行文本,首行是QAbstractButton的text显示,次行类似于副标题,是通过CommandLinkButton的description属性来设置的
.默认情况下,它还会带有一个向右的箭头图标,该图标实际上就是QAbstractButton的Icon设置图标,只是填了一个右箭头作为缺省值
.默认类似于一个扁平化自动凸起的按钮
QCommandLinkButton命令链接按钮控件的python代码
# Form implementation generated from reading ui file 'QCommandLinkButton命令链接按钮控件.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.commandLinkButton = QtWidgets.QCommandLinkButton(parent=Form)
self.commandLinkButton.setGeometry(QtCore.QRect(90, 60, 186, 101))
self.commandLinkButton.setAutoDefault(False)
self.commandLinkButton.setDefault(False)
self.commandLinkButton.setObjectName("commandLinkButton")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.commandLinkButton.setText(_translate("Form", "www.baidu.com"))
self.commandLinkButton.setDescription(_translate("Form", "百度"))
QCommandLinkButton命令链接按钮控价的加载ui文件的python代码
import sys
from PyQt6.QtWidgets import QApplication, QCommandLinkButton
from PyQt6 import uic
if __name__ == '__main__':
app = QApplication(sys.argv)
ui = uic.loadUi('./QCommandLinkButton命令链接按钮控件.ui')
myQCommandLinkButton:QCommandLinkButton = ui.commandLinkButton
ui.show()
sys.exit(app.exec())
QRadioButton单选按钮控件
QRadioButton是单选按钮控件,它为用户提供由两个或多个互斥选项组成的选项集,当用户选中某单选按钮时,同一组中的其他单选按钮不能同时选定
QRadioButton类的常用方法
setText():设置单选按钮显示的文本
text():获取单选按钮显示的文本
setCheckde()或者setCheckable():设置单选按钮是否为选中状态,True为选中状态,False为未选中状态
isChecked():返回单选按钮的状态,True为选中状态,False为未选中状态
QRadioButton单选按钮控件的python代码
# Form implementation generated from reading ui file 'QRadioButton单选按钮控件.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.radioButton = QtWidgets.QRadioButton(parent=Form)
self.radioButton.setGeometry(QtCore.QRect(30, 40, 41, 20))
self.radioButton.setCheckable(True)
self.radioButton.setChecked(False)
self.radioButton.setAutoRepeat(False)
self.radioButton.setAutoExclusive(True)
self.radioButton.setObjectName("radioButton")
self.radioButton_2 = QtWidgets.QRadioButton(parent=Form)
self.radioButton_2.setGeometry(QtCore.QRect(150, 40, 41, 20))
self.radioButton_2.setChecked(False)
self.radioButton_2.setObjectName("radioButton_2")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.radioButton.setText(_translate("Form", "男"))
self.radioButton_2.setText(_translate("Form", "女"))
QRadioButton单选按钮加载ui文件的python代码
import sys
from PyQt6.QtWidgets import QApplication, QRadioButton
from PyQt6 import uic
if __name__ == '__main__':
app = QApplication(sys.argv)
ui = uic.loadUi('./QRadioButton单选按钮控件.ui')
myQRadioButton: QRadioButton = ui.radioButton
ui.show()
sys.exit(app.exec())
QCheckBox复选框按钮控件
QCheckBox是复选框控件,它用来表示是否选取了某个选项条件,常用于为用户提供具有(是/否)或(真/假)值的选项
QCheckBox控件的使用与QRadioButton控件类似,但它是为用户提供了“多选多”的选择,另外,它除了选中和未选中两种状态之外,还提供了第三种状态:半选中,如果需要第三种状态。需要使用QCheckBox类的setTristate()方法使其生效,并且可以使用checkState()方法查询当前状态
Checkbox控件的三种状态值及说明
Qt.Checked-选中
Qt.PartiallyChecked-半选中
Qt.Unchecked-未选中
QCheckBox复选框按钮控件的pyhton代码
# Form implementation generated from reading ui file 'QCheckBox复选框按钮控件.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.checkBox = QtWidgets.QCheckBox(parent=Form)
self.checkBox.setGeometry(QtCore.QRect(90, 90, 79, 20))
self.checkBox.setTristate(True)
self.checkBox.setObjectName("checkBox")
self.checkBox_2 = QtWidgets.QCheckBox(parent=Form)
self.checkBox_2.setGeometry(QtCore.QRect(90, 130, 79, 20))
self.checkBox_2.setTristate(True)
self.checkBox_2.setObjectName("checkBox_2")
self.checkBox_3 = QtWidgets.QCheckBox(parent=Form)
self.checkBox_3.setGeometry(QtCore.QRect(90, 180, 79, 20))
self.checkBox_3.setTristate(True)
self.checkBox_3.setObjectName("checkBox_3")
self.checkBox_4 = QtWidgets.QCheckBox(parent=Form)
self.checkBox_4.setGeometry(QtCore.QRect(90, 220, 79, 20))
self.checkBox_4.setTristate(True)
self.checkBox_4.setObjectName("checkBox_4")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.checkBox.setText(_translate("Form", "吃饭"))
self.checkBox_2.setText(_translate("Form", "睡觉"))
self.checkBox_3.setText(_translate("Form", "学习"))
self.checkBox_4.setText(_translate("Form", "运动"))
QCheckBox复选框按钮控件加载ui文件的python代码
import sys
from PyQt6.QtWidgets import QApplication, QCheckBox
from PyQt6 import uic
if __name__ == '__main__':
app = QApplication(sys.argv)
ui = uic.loadUi('./QCheckBox复选框按钮控件.ui')
myQCheckBox: QCheckBox = ui.checkBox
ui.show()
sys.exit(app.exec())
QDialogButtonBox组合按钮控件
QDialogButtonBox是一个包含很多按钮的控件,对话框中有多个需要分组排列的按钮时,可以使用QDialogButtonBox类,开发人员可以向QDialogButtonBox添加按钮,QDialogButtonBox会根据平台自动使用合适的布局
QDialogButtonBox组合按钮控件的python代码:
# Form implementation generated from reading ui file 'QDialogButtonBox组合按钮控件.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(829, 615)
self.buttonBox = QtWidgets.QDialogButtonBox(parent=Form)
self.buttonBox.setGeometry(QtCore.QRect(30, 20, 681, 501))
self.buttonBox.setOrientation(QtCore.Qt.Orientation.Vertical)
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.StandardButton.Abort|QtWidgets.QDialogButtonBox.StandardButton.Apply|QtWidgets.QDialogButtonBox.StandardButton.Cancel|QtWidgets.QDialogButtonBox.StandardButton.Close|QtWidgets.QDialogButtonBox.StandardButton.Discard|QtWidgets.QDialogButtonBox.StandardButton.Help|QtWidgets.QDialogButtonBox.StandardButton.Ignore|QtWidgets.QDialogButtonBox.StandardButton.No|QtWidgets.QDialogButtonBox.StandardButton.NoToAll|QtWidgets.QDialogButtonBox.StandardButton.Ok|QtWidgets.QDialogButtonBox.StandardButton.Open|QtWidgets.QDialogButtonBox.StandardButton.Reset|QtWidgets.QDialogButtonBox.StandardButton.RestoreDefaults|QtWidgets.QDialogButtonBox.StandardButton.Retry|QtWidgets.QDialogButtonBox.StandardButton.Save|QtWidgets.QDialogButtonBox.StandardButton.SaveAll|QtWidgets.QDialogButtonBox.StandardButton.Yes|QtWidgets.QDialogButtonBox.StandardButton.YesToAll)
self.buttonBox.setCenterButtons(True)
self.buttonBox.setObjectName("buttonBox")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
QDialogButtonBox组合按钮控件加载ui文件的python代码:
import sys
from PyQt6.QtWidgets import QApplication, QDialogButtonBox
from PyQt6 import uic
if __name__ == '__main__':
app = QApplication(sys.argv)
ui = uic.loadUi('./QDialogButtonBox组合按钮控件.ui')
myQDialogButtonBox: QDialogButtonBox = ui.buttonBox
ui.show()
sys.exit(app.exec())
选择列表类控件
选择列表类控件主要以列表形式为用户提供选择的项目,用户可以从中选择项,本节将对PyQt6中的常用选择列表类控件的使用进行讲解,包括QComboBox、QFontComboBox和QListWidget等
QComboBox下拉组合框控件
QComboBox控件,又称为下拉组合框控件,它主要用于在下拉组合框中显示数据,用户可以从中选择项
1.【read-only】count:const int
组合框中的项目数
2.【read-only】currentData:const QVariant
当前项目的数据
3.currentindex:int
组合框中当前项目的索引,插入或删除项目时,当前索引可以更改
4.currentText:QString
当前文本,如果组合框是可编辑的,则当前文本是行编辑显示的值
如果组合框是可编辑的,setCurrentText()只会调用setEditText(),否则,如果列表中有匹配的文本,则currentIndex设置为相应的索引
5.duplicatesEnabled:bool
可编辑时设置setSizeAdjustPolicy()将输入内容插入列组合框,是否可以在组合框中插入重复项,默认为false
请注意,始终可以以编程方式将重复项插入组合框
6.editable:bool
是否可以编辑组合框,默认为false,编辑的效果取决于插入策略
注意:禁用可编辑状态时,将删除验证器和完成器
7.frame:bool
略
8.iconSize:QSize
组合框中显示的图标的大小,这是图标可以具有的最大大小,较小尺寸的图标不会放大
9.insertPolicy:InsertPolicy
可编辑时插入的项目应出现在组合框中的位置的策略
默认值为InsertAtBottom,即新项目将出现在项目列表的底部
10.maxCount:int
组合框中允许的最大项目数
如果将最大数量设置为小于组合框中当期项目的数量,则额外的项目奖被截断,如果在组合框上设置了外部模型,这也适用
11.maxVisibleItems:int
组合框屏幕上允许的最大项目数,默认为10
对于QStyle::SH_ComboBox_Popup返回true的样式中的不可编辑组合框,将忽略此属性
12.minimumContentsLength:int
应适合组合框的最小字符,默认值为0
如果此属性设置为正值,则minimumSizeHint()和sizeHint()会将其考虑此属性在内
13.modelColumn:int
模型中可见的列,默认为0
14.placeholderText:QString
未设置有效索引时显示的占位符文本
当设置无效索引时将显示占位符文本,下拉列表中的文本不可访问,在添加项目之前调用此函数时,将显示占位符文本,否则如果要显示占位符文本,则必须以编程的方式调用setCurrentIndex(-1)
当QComboBox可编辑时,请改用lineEdit()->setPlaceholderText()
15.sizeAdjustPolicy:SizeAdjustPolicy
内容更改时组合框大小如何更改的策略,默认值为AdjustToContentsOnFirstShow
QComboBox类的常用方法
additem():添加一个下拉列表项
additems():从列表中添加下拉框选项
currentText():获取选中项的文本
currentIndex():获取选中项的索引
itemText(index):获取索引为index的项的文本
setItemText(index,text):设置索引为index的项的文本
count():获取所有选项的数量
clear():删除所有选项
ui加载的代码
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<widget class="QComboBox" name="comboBox">
<property name="geometry">
<rect>
<x>100</x>
<y>80</y>
<width>68</width>
<height>22</height>
</rect>
</property>
<property name="editable">
<bool>false</bool>
</property>
<property name="maxVisibleItems">
<number>6</number>
</property>
<property name="maxCount">
<number>2147483647</number>
</property>
<property name="placeholderText">
<string/>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>
QComboBox下拉组合框控件的加载ui文件的python代码
import sys
from PyQt6.QtGui import QIcon
from PyQt6.QtWidgets import QApplication, QComboBox
from PyQt6 import uic
if __name__ == '__main__':
app = QApplication(sys.argv)
ui = uic.loadUi('./QComboBox下拉组合框控件.ui')
myQComboBox: QComboBox = ui.comboBox
# myQComboBox.addItem('足球')
# list = ['篮球', '乒乓球', '羽毛球']
# myQComboBox.addItems(list)
#
# myQComboBox.addItem(QIcon('Other.png'), '其他')
name_list = ['足球', '篮球', '乒乓球', '羽毛球', '其他']
png_list = ['足球.png', '篮球.png', '乒乓球.png', '羽毛球.png', 'Other.png']
for name, icon in zip(name_list, png_list):
myQComboBox.addItem(QIcon(icon), name)
print(myQComboBox.currentText(), myQComboBox.currentIndex()) # 获取选中项的文本和索引
print(myQComboBox.itemText(1)) # 获取索引为index项的文本
myQComboBox.setItemText(1, '给篮球改下名') # 根据索引改名
print(myQComboBox.count()) # 获取所有项的数量
ui.show()
sys.exit(app.exec())
QFontComboBox字体组合框控件
QFontComboBox控件又称为字体组合框控件,它主要用于在下拉组合框中显示并选择字体
QFontComboBox继承来自QComboBox,自己拥有三个属性,分别是writingSystem,fontFilters,currentFont
writingSystem是书写系统,书写系统包括一个或多个文字集和一系列规则,一个书写系统至少对应一种语言,可以用书写系统的符号集合和规则,比如拼写规则、大小写等来表达对对应的语言,如汉字、日文、罗马字等
fontFilters是文字过滤器,以QFontComboBox类中提供了一个setFontFilters()方法,用来设置可以选择的字体,该方法的参数值及说明如下
-QFontComboBox.AllFonts:所有字体
-QFontComboBox.ScalableFonts:可以自动伸缩的字体
-QFontComboBox.NonScalableFonts:不自动伸缩的字体
-QFontComboBox.MonospacedFonts:等宽字体
QFontComboBox.ProportionalFonts:比例字体
currentFont当前字体
ui生成代码
# Form implementation generated from reading ui file 'QFontComboBox字体组合框控件.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.fontComboBox = QtWidgets.QFontComboBox(parent=Form)
self.fontComboBox.setGeometry(QtCore.QRect(110, 90, 224, 22))
self.fontComboBox.setWritingSystem(QtGui.QFontDatabase.WritingSystem.SimplifiedChinese)
self.fontComboBox.setFontFilters(QtWidgets.QFontComboBox.FontFilter.AllFonts)
self.fontComboBox.setObjectName("fontComboBox")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
QFontComboBox字体组合框控件加载ui文件的python代码
import sys
from PyQt6.QtWidgets import QApplication, QFontComboBox
from PyQt6 import uic
if __name__ == '__main__':
app = QApplication(sys.argv)
ui = uic.loadUi('./QFontComboBox字体组合框控件.ui')
myQFontComboBox: QFontComboBox = ui.fontComboBox
print(myQFontComboBox.currentText(), myQFontComboBox.currentIndex())
ui.show()
sys.exit(app.exec())
QListWidget列表控件
PyQt6提供了两种列表,分别是QListWidget和QListView,其中,QListView是基于模型的,它是QListWidget的父类,使用QListView时,首先需要建立模型,然后保存数据;而QListWidget是QListView的升级版本,它已经内置了一个数据存储模型QListWidgetitem,我们在使用时,不必自己建立模型,而直接使用additem()或者additems()方法即可添加列表项,所以在实际开发时,推荐使用QListWidget控件作为列表
QListWidget常用方法
additem():向列表中添加项
additems():一次性向列表中添加多项
insertitem():在指定索引处插入项
setCurrentitem():设置当前选择项
item.setToolTip():设置提示内容
item.isSelected():判断是否选中
setSelectionMode():
设置列表的选择模式,支持以下5种模式:
QAbstractitemView.NoSelection:不能选择
QAbstractitemView.SingleSelection:单选
QAbstractitemView.MultiSelection:多选
QAbstractitemView.ExtendedSelection:正常单选,按Ctrl或者Shift键后,可以多选
QAbstractitemView.ContiguousSelection:与ExtendedSelection类似
setSelectionBehavior():
设置选择项的方式,支持以下3种方式:
QAbstractitemView.Selectitems:选中当前项
QAbstractitemView.SelectRows:选中整行
QAbstractitemView.SelectColumns:选中整列
setWordWrap():设置是否自动换行,True表示自动换行,False表示不自动换行
setViewMode():
设置显示模式,有以下两种显示模式
QListView.ListMode:以列表形式显示
QListView.IconMode:以图表形式显示
Item.text():获取项的文本
clear():删除所有列表项