4、Qt-pyqt6常用基本控件 - 文本类控件
控件
对应QTDesigner中的 左侧控件
- Layouts -- 布局管理
控件名 | 说明 |
---|---|
VerticalLayout | 垂直布局 |
HorizontalLayout | 水平布局 |
GridLayout | 网格布局 |
FormLayout | 表单布局 |
- Spacers -- 弹簧
控件名 | 说明 |
---|---|
HoriziontalSpacer | 水平弹簧 |
VerticalSpacer | 垂直弹簧 |
- Buttons -- 按钮类
控件名 | 说明 |
---|---|
PushButton | 按钮 |
ToolButton | 工具按钮 |
RadioButton | 单选按钮 |
CheckBox | 复选框 |
CommandLinkButton | 命令链接按钮 |
DialogButtonBox | 对话框按钮盒 |
- Item Views(Model-Based) -- 项目视图
控件名 | 说明 |
---|---|
ListView | 列表视图 |
TreeView | 树视图 |
TableView | 表格视图 |
ColumnView | 列视图 |
UndoView | 撤销命令显示视图 |
- Item Widgets(Item-Based) -- 项目控件
控件名 | 说明 |
---|---|
ListWidget | 列表控件 |
TreeWidget | 树控件 |
TableWideget | 表格控件 |
- Containers -- 容器
控件名 | 说明 |
---|---|
GroupBox | 分组框 |
ScrollArea | 滚动区域 |
ToolBox | 工具箱 |
TabWidget | 选项卡 |
StackedWidget | 堆栈窗口 |
Frame | 帧 |
Widget | 小部件 |
MDIArea | MDI区域 |
DockWidget | 停靠窗口 |
- Input Widgets -- 输入控件
控件名 | 说明 |
---|---|
ComboBox | 下拉组合框 |
FontComboBox | 字体组合框 |
LineEdit | 单行文本编辑框 |
TextEdit | 多行文本编辑框 |
PlainTextEdit | 纯文本编辑框 |
SpinBox | 数字选择控件 |
DoubleSpinBox | 小数选择控件 |
TimeEdit | 时间编辑框 |
DateEdit | 日期编辑框 |
DataTimeEdit | 日期时间编辑框 |
Dial | 旋钮 |
HorizontalScrollBar | 横向滚动条 |
VerticalScrollBar | 垂直滚动条 |
HorizontalSlider | 横向滑块 |
VerticalSlider | 垂直滑块 |
KeySequenceEdit | 按钮编辑框 |
- Display Widgets -- 显示控件
控件名 | 说明 |
---|---|
Label | 标签控件 |
Text Browser | 文本浏览器 |
Graphics View | 图形视图 |
Valendar Widget | 日历控件 |
LCDNumber | 液晶数字显示 |
Progress Bar | 进度条 |
HorizontalLine | 水平线 |
Vertical Line | 垂直线 |
OpenGLWidget | 开放式图形库工具 |
QLabel 使用案例
- 标签文本自动换行
其代码为:
self.label.setWordWrap(True)
- 点击标签跳转链接
其代码为:
self.label.setOpenExternalLinks(True) #打开外部链接功能
- 为标签设置图片
其代码为:
self.label.setPixmap(QtGui.QPixmap('图片路径'))
- 获取标签文本
# 获取ui文件中的label控件 mylabel = ui.label print(mylabel.text())
- 完整的代码:
from PyQt6.QtWidgets import QApplication from PyQt6 import uic import sys if __name__ == '__main__': app = QApplication(sys.argv) # 创建应用程序对象 # 加载ui文件 ui = uic.loadUi('./标签demo.ui') # 获取ui文件中的label控件 #mylabel = ui.label mylabel: QLabel = ui.label # 类型提示 注释 print(mylabel.text()) # 显示窗口(将ui文件内容显示) ui.show() sys.exit(app.exec()) # app.exec()进入无限消息循环,监听用户动作
📃 Input Widgets中的单行文本框(Line Edit)
- 使用属性如下:
QLineEdit是单行文本框,该控件只能输入单行字符串。该类的常用方法如下:
方法 | 说明 |
---|---|
setText() | 设置文本框内容 |
text() | 获取文本框内容 |
setPlaceholderText() | 设置文本框浮显文字 |
setMaxLength() | 设置允许在文本框内输入的最大长度 |
setAlignment() | 设置文本对齐方式 |
setReadOnly() | 设置文本框只读 |
setFocus() | 使文本框得到焦点 |
setEchoMode() | 设置文本框显示字符的模式,有以下4种模式。 QLineEdit.Normal:正常显示输入的字符,这是默认设置; QLineEdit.NoEcho:不显示任何输入的字符(不显示输入,只显示); QLineEdit.Password:显示与平台相关的密码掩码字符,而不是实际输入的字符; QLineEdit.PasswordEchoOnEdit:在编辑时显示字符,失去焦点后显示密码掩码字符 |
setValidator() | 设置文本框输入验证器,有以下3种模式。 QIntValidator:限制输入整数; QDoubleValidator:限制输入小数; QRegExpValidator:检查输入是否符合设置的正则表达式 |
setInputMask() | 设置掩码,掩码通常由掩码字符和分隔符组成,后面可以跟一个分号和空白字符,空白字符在编辑完成后会从文本框中删除,常用的掩码字符如下: 日期掩码:0000-00-00; 时间掩码:00:00; 序列号掩码:AAAAA-AAAAA-AAAAA-AAAAA-AAAAA;# |
clear() | 清除文本框内容 |
案例:
- 先画一个:
Input Widgets
中的Line Edit
,生成.ui文件,在pycharm
中转为.py
文件方便查看函数属性,main.py
代码如下:
from PyQt6.QtWidgets import QApplication, QLabel from PyQt6 import uic import sys if __name__ == '__main__': app = QApplication(sys.argv) # 创建应用程序对象 # 加载ui文件 ,ui变量相当于是LinrText.py文件中的setupUi函数 ui = uic.loadUi('./LineText.ui') # 获取ui文件中的控件,ui.lineEdit详单于:方法.属性 myLineEdit = ui.lineEdit print(myLineEdit.text()) # 获取文本框内容 # 显示窗口(将ui文件内容显示) ui.show() sys.exit(app.exec()) # app.exec()进入无限消息循环,监听用户动作
完整实例代码
LineText.py
# Form implementation generated from reading ui file 'LineText.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(401, 300) self.lineEdit = QtWidgets.QLineEdit(parent=Form) self.lineEdit.setGeometry(QtCore.QRect(100, 40, 131, 31)) self.lineEdit.setText("") self.lineEdit.setObjectName("lineEdit") self.lineEdit_2 = QtWidgets.QLineEdit(parent=Form) self.lineEdit_2.setGeometry(QtCore.QRect(100, 80, 131, 31)) self.lineEdit_2.setEchoMode(QtWidgets.QLineEdit.EchoMode.Password) self.lineEdit_2.setObjectName("lineEdit_2") self.lineEdit_3 = QtWidgets.QLineEdit(parent=Form) self.lineEdit_3.setGeometry(QtCore.QRect(110, 140, 113, 21)) self.lineEdit_3.setObjectName("lineEdit_3") self.lineEdit_4 = QtWidgets.QLineEdit(parent=Form) self.lineEdit_4.setGeometry(QtCore.QRect(110, 200, 181, 41)) self.lineEdit_4.setObjectName("lineEdit_4") self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) self.lineEdit.setPlaceholderText(_translate("Form", "请输入用户名")) self.lineEdit_2.setPlaceholderText(_translate("Form", "请输入密码")) self.lineEdit_3.setInputMask(_translate("Form", "0000-00-00")) self.lineEdit_4.setPlaceholderText(_translate("Form", "文本验证器setValidator()"))
main.py
from PyQt6.QtGui import QIntValidator from PyQt6.QtWidgets import QApplication, QLabel from PyQt6 import uic import sys if __name__ == '__main__': app = QApplication(sys.argv) # 创建应用程序对象 # 加载ui文件 ,ui变量相当于是LinrText.py文件中的setupUi函数 ui = uic.loadUi('./LineText.ui') # 获取ui文件中的控件,ui.lineEdit详单于:方法.属性 myLineEdit = ui.lineEdit # 第一个文本框 print(myLineEdit.text()) # 获取文本框内容 myLineEdit.clear() # 清空文本框内容 # 设置焦点,默认光标在第二个文本框中 myLineEdit2 = ui.lineEdit_2 myLineEdit2.setFocus() # 设置掩码 myLineEdit3 = ui.lineEdit_3 myLineEdit3.setInputMask('9999-99-99') # 文本验证器 mylineEdit_4 = ui.lineEdit_4 mylineEdit_4.setValidator(QIntValidator()) # 只能输入整数 # 显示窗口(将ui文件内容显示) ui.show() sys.exit(app.exec()) # app.exec()进入无限消息循环,监听用户动作
LineText.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>401</width> <height>300</height> </rect> </property> <property name="windowTitle"> <string>Form</string> </property> <widget class="QLineEdit" name="lineEdit"> <property name="geometry"> <rect> <x>100</x> <y>40</y> <width>131</width> <height>31</height> </rect> </property> <property name="text"> <string/> </property> <property name="placeholderText"> <string>请输入用户名</string> </property> </widget> <widget class="QLineEdit" name="lineEdit_2"> <property name="geometry"> <rect> <x>100</x> <y>80</y> <width>131</width> <height>31</height> </rect> </property> <property name="echoMode"> <enum>QLineEdit::Password</enum> </property> <property name="placeholderText"> <string>请输入密码</string> </property> </widget> <widget class="QLineEdit" name="lineEdit_3"> <property name="geometry"> <rect> <x>110</x> <y>140</y> <width>113</width> <height>21</height> </rect> </property> <property name="inputMask"> <string>0000-00-00</string> </property> </widget> <widget class="QLineEdit" name="lineEdit_4"> <property name="geometry"> <rect> <x>110</x> <y>200</y> <width>181</width> <height>41</height> </rect> </property> <property name="placeholderText"> <string>文本验证器setValidator()</string> </property> </widget> </widget> <resources/> <connections/> </ui>
📚 多行富文本框 -- Input Widgets/Text Edit
Input Widgets/Text Edit 是多行文本框控件,主要用来显示多行的文本内容,当文本内容超出控件的显示范围时,该控件将显示垂直滚动条,另外,还能显示HTML网页
- 常用方法如下:
方法 | 描述 |
---|---|
setPlainText() | 设置文本内容 |
toPlainText() | 获取文本内容 |
setTextColor() | 设置文本颜色,如红色,QtGui.QColor(255,0,0) |
setTextBackgroundColor() | 设置文本的背景色,颜色参数与setTextColor()相同 |
setHtml() | 设置HTML文档内容 |
toHtml() | 获取HTML文档内容 |
setLineWrapMode | 文本自动换行 ,有四种模式 |
clear() | 清除所有内容 |
overwriteMode() | 用于控制用户输入文本是否替换现有文本, 如果为True,则输入字符从当前光标位置开始逐一替换当前的字符 为False:则在光标处插入字符。可以通过方法:overwriteMode(),setOverwriteMode()进行访问 |
先画一个ui文件
代码如下:
TextEdit.py
# Form implementation generated from reading ui file 'TextEdit.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(583, 402) self.textEdit = QtWidgets.QTextEdit(parent=Form) self.textEdit.setGeometry(QtCore.QRect(20, 50, 201, 141)) self.textEdit.setObjectName("textEdit") self.textEdit_2 = QtWidgets.QTextEdit(parent=Form) self.textEdit_2.setGeometry(QtCore.QRect(280, 50, 211, 141)) self.textEdit_2.setObjectName("textEdit_2") self.label = QtWidgets.QLabel(parent=Form) self.label.setGeometry(QtCore.QRect(70, 200, 53, 15)) self.label.setObjectName("label") self.label_2 = QtWidgets.QLabel(parent=Form) self.label_2.setGeometry(QtCore.QRect(350, 200, 71, 21)) 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.label.setText(_translate("Form", "纯文本")) self.label_2.setText(_translate("Form", "html网页"))
main.py
from PyQt6.QtGui import QIntValidator from PyQt6.QtWidgets import QApplication, QLabel from PyQt6 import uic, QtGui import sys if __name__ == '__main__': app = QApplication(sys.argv) # 创建应用程序对象 # 加载ui文件 ,ui变量相当于是LinrText.py文件中的setupUi函数 ui = uic.loadUi('./TextEdit.ui') # 获取文本框对象 myQTextEdit_1 = ui.textEdit myQTextEdit_2 = ui.textEdit_2 # 设置文本颜色 myQTextEdit_1.setTextColor(QtGui.QColor(255, 0, 0)) # myQTextEdit_2.setTextColor(QtGui.QColor(255, 0, 0)) #html文本颜色不能这样设置 # 设置文本背景色 myQTextEdit_1.setTextBackgroundColor(QtGui.QColor(255, 255, 0)) myQTextEdit_1.setPlainText("这是纯文本") # 设置html文本 myQTextEdit_2.setHtml("学习<font color='red'>PyQt6</font></br><a href='https://www.cnblogs.com/littlecc'>查看这个blog</a>") # 获取文本内容 print(myQTextEdit_1.toPlainText()) print(myQTextEdit_2.toHtml()) # 清空文本 # myQTextEdit_1.clear() # myQTextEdit_2.clear() # 显示窗口(将ui文件内容显示) ui.show() sys.exit(app.exec()) # app.exec()进入无限消息循环,监听用户动作
TextEdit.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>583</width> <height>402</height> </rect> </property> <property name="windowTitle"> <string>Form</string> </property> <widget class="QTextEdit" name="textEdit"> <property name="geometry"> <rect> <x>20</x> <y>50</y> <width>201</width> <height>141</height> </rect> </property> </widget> <widget class="QTextEdit" name="textEdit_2"> <property name="geometry"> <rect> <x>280</x> <y>50</y> <width>211</width> <height>141</height> </rect> </property> </widget> <widget class="QLabel" name="label"> <property name="geometry"> <rect> <x>70</x> <y>200</y> <width>53</width> <height>15</height> </rect> </property> <property name="text"> <string>纯文本</string> </property> </widget> <widget class="QLabel" name="label_2"> <property name="geometry"> <rect> <x>350</x> <y>200</y> <width>71</width> <height>21</height> </rect> </property> <property name="text"> <string>html网页</string> </property> </widget> </widget> <resources/> <connections/> </ui>
📘 纯文本控件 Input Widgets/Plain Text Edit
主要用来显示多行的文本内容,会自动换行,会自动生成滚动条
常用属性
方法 | 描述 |
---|---|
setPlainText() | 设置文本内容 |
toPlainText() | 获取文本内容 |
setLineWrapMode() | lineWrapMode:lineWrapMode属性用于控制换行模式 其类型为枚举类型QTextEdit.LineWrapMode,缺省值为WidgetWidth,表示以词为单位在编辑器右边换行,换行出现在空白处,保持整个单词的完整性。 可以调用方法lineWrapMode()、setLineWrapMode()来访问该属性。 |
clear() | 清除所有内容 |
overwriteMode() | overwriteMode属性用于控制用户输入文本是否替换现有文本, 如果为True,则输入字符从当前光标位置开始逐一替换当前的字符为False则在光标处插入输入字符。 缺省值为False,可以通过方法overwriteMode()、setOverwriteMode()进行访问 |
🔢 整数数字选择控件 -- Input Widgets/Spin Box
该控件提供一堆上下箭头,用户可以单击箭头选择数值,也可以直接输入,如果输入的数值大于设置的最大值或者最小值,Spin Box将不会接受输入
Spin Box类常用的方法如下:
方法 | 描述 |
---|---|
SetValue() | 设置控件的当前值 |
setMaximum() | 设置最大值 |
setMinMum() | 设置最小值 |
setRange() | 设置取值范围(包括最大值和最小值) |
setSingleStep() | 设置单击箭头时的步长值 |
value() | 获取控件中的值 |
setStepType() | QAbstractSpinBox::DefaultStepType:固定步长,可以通过setSingleStep()函数设置 。 QAbstractSpinBox::AdaptiveDecimalStepType:自适应步长,根据数字的大小自动调整步长。 |
setPrefix() | 设置后缀 |
setSuffix() | 设置后缀 |
设置生成的.ui文件如下:
SpinBox.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>543</width> <height>416</height> </rect> </property> <property name="windowTitle"> <string>Form</string> </property> <widget class="QSpinBox" name="spinBox"> <property name="geometry"> <rect> <x>50</x> <y>130</y> <width>131</width> <height>51</height> </rect> </property> <property name="suffix"> <string>件</string> </property> <property name="prefix"> <string>第</string> </property> <property name="maximum"> <number>20</number> </property> <property name="singleStep"> <number>2</number> </property> </widget> </widget> <resources/> <connections/> </ui>
转为.py文件如下:
SpinBox.py
# Form implementation generated from reading ui file 'SpinBox.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(543, 416) self.spinBox = QtWidgets.QSpinBox(parent=Form) self.spinBox.setGeometry(QtCore.QRect(50, 130, 131, 51)) self.spinBox.setMaximum(20) self.spinBox.setSingleStep(2) self.spinBox.setObjectName("spinBox") 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", "第"))
主函数
main.py
from PyQt6.QtGui import QIntValidator from PyQt6.QtWidgets import QApplication, QLabel from PyQt6 import uic, QtGui import sys if __name__ == '__main__': app = QApplication(sys.argv) # 创建应用程序对象 # 加载ui文件 ,ui变量相当于是LinrText.py文件中的setupUi函数 ui = uic.loadUi('./SpinBox.ui') # 获取SpinBox控件 myQSpinBox = ui.spinBox # 显示窗口(将ui文件内容显示) ui.show() sys.exit(app.exec()) # app.exec()进入无限消息循环,监听用户动作
🅾 小数数字选择控件 - Input Widgets/Double Spin Box
与 上面的整数数字选择控件 一样 ,多了一个,
setDecimals()
方法用来设置小鼠的位数
默认保留两位小数
🖥 液晶数字显示控件 -- Display Widgets/LCD Number
用来显示液晶数字
常用的方法如下:
方法 | 描述 |
---|---|
setDigitCount() | 设置可以显示的数字数量 |
setSmallDecimalPoint() | 是否有小数点,如果有小数点就可以显示 |
setMode() | 设置显示数字的模式,有四种模式: Bin(二进制),Oct(八进制),Dec(十进制),Hex(十六进制) |
setSegmentstyle() | 设置显示样式,有3种样式:OutLine,Filled 和 Flat |
Value() | 获取显示的数值 |
本文作者:little小新
本文链接:https://www.cnblogs.com/littlecc/p/18340373
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步