3、 展示控件
3.1 QLabel
3.1.1 描述
提供了文本或图像的显示
但是没有提供用户交互功能,继承自QFrame
3.1.2 功能作用
3.1.2.1 基本功能
| |
| |
| |
| from PyQt5.Qt import * |
| import sys |
| |
| app = QApplication(sys.argv) |
| w = QWidget() |
| w.resize(500, 500) |
| |
| l = QLabel("标签哦", w) |
| """ |
| l.adjustSize() # 根据内容适应标签大小 |
| l.setStyleSheet("background-color: skyblue;") |
| |
| 对齐 |
| l.setAlignment(Qt.AlignLeft | Qt.AlignVCenter) |
| |
| 缩进和边距 |
| l.setIndent(20) |
| l.setMargin(20) |
| |
| 文本格式 |
| l.setTextFormat(Qt.TextFormat) # PlainText/ RichText/ AutoText/ MarkdownText |
| |
| 小伙伴 |
| l.setBuddy(QWidget) # 参数是要关联的输入框,当点击快捷键是可以快速定位到相应的文本框上面 |
| 内容缩放 |
| l.setScaledContents(True) # 设置图片内容的缩放,适应控件大小,仅限于图片 |
| |
| 打开文本链接 |
| l.setOpenExternalLinks(True) # 使得可以打开超链接 |
| |
| 单词换行 |
| l.setWordWrap(True) # 设置单词换行,同时也可以使用 \n 换行 |
| """ |
| |
| w.show() |
| sys.exit(app.exec_()) |
3.1.2.2 文本交互
| |
| setTextInteractionFlags(Qt.TextInteractionFlags) |
| textInteractionFlags() |
| |
| |
| setSelection(int start, int length) |
| hasSelection() |
| selectedText() |
| selectionStart() |
Qt.TextInteractionFlags
:
Qt.NoTextInteraction
:不可能进行交互
Qt.TextSelectableByMouse
:可以使用鼠标选择文本并使用上下文菜单或标准键盘快捷键将其复制到剪切板
Qt.TextSelectionByKeyboard
:可以使用键盘上的光标键选择文本,显示文本光标
Qt.LinksAccessibleByMouse
:可以使用鼠标突出显示和激活链接
Qt.LinksAccessibleByKeyborad
:可以使用选项卡聚焦链接并使用enter激活
Qt.TextEditable
:改文字完全可以编辑
Qt.TextEditiorInteraction
:文本编辑器的默认值;TextSelectableByMouse | TextSelectionByKeyboard | TextEditable
Qt.TextBrowserInteraction
:文本浏览器的默认值; TextSelectableByMouse | TextSelectionByKeyboard | LinksAccessibleByMouse | LinksAccessibleByKeyborad
3.1.2.3 内容操作
3.1.2.3.1 文本字符串
3.1.2.3.2 数值数据
3.1.2.3.3 图形图像
| setPicture(QPicture) |
| setPixmap(QPixmap) |
3.1.2.3.4 动图
此类用于显示没有声音的简单动画
常用操作:
| setScaledSize(QSize) |
| setPaused(bool) |
| setSpeed(int) |
| start() |
3.1.2.3.5 清空
3.1.3 信号
| |
| linkActivated(link_str) |
| linkHovered(link_str) |
3.2 QLCDNumber
3.2.1 描述
展示LCD样式的数字,它可以展示几乎任何大小的数字;它可以显示十进制、十六进制、八进制或二进制
继承自QFrame
3.2.2 功能作用
3.2.2.1 基本使用
| |
| |
| |
| from PyQt5.Qt import * |
| import sys |
| |
| app = QApplication(sys.argv) |
| w = QWidget() |
| w.resize(500, 500) |
| |
| ql = QLCDNumber(w) |
| ql.resize(100, 50) |
| ql.display("12345") |
| print(ql.value()) |
| ql.setDigitCount(5) |
| w.show() |
| sys.exit(app.exec_()) |
3.2.2.2 模式设置
| setMode(QLCDNumber.Mode) |
| mode() |
| |
| setHexMode() |
| setDecMode() |
| setOctMode() |
| setBinMode() |
3.2.2.3 溢出判定
| checkOverflower(float/ int) |
3.2.2.4 分段样式
| setSegmentStyle(QLCDNumber.SegmentStyle) |
| segmentStyle() |
QLCDNumber.SegmentStyle
:
QLCDNumber.Outline
:生成填充了背景颜色的凸起部分
QLCDNumber.Filled
:默认值,生成填充前景色的凸起部分
QLCDNumber.Flat
:生成填充前景色的平坦段
3.2.3 信号
3.3 QProgressBar
3.3.1 描述
提供一个水平或垂直进度条;进度条用于向用户提供操作进度提示,并向他们保证应用程序仍在运行
继承自 QWidget
3.3.2 功能作用
3.3.2.1 基本功能
| |
| |
| |
| from PyQt5.Qt import * |
| import sys |
| |
| app = QApplication(sys.argv) |
| w = QWidget() |
| w.resize(500, 500) |
| |
| qp = QProgressBar(w) |
| qp.setMaximum(100) |
| qp.setMinimum(0) |
| |
| qp.setValue(20) |
| |
| print(qp.value()) |
| |
| |
| |
| |
| |
| |
| |
| w.show() |
| sys.exit(app.exec_()) |
3.3.2.2 格式设置
| setFormat(str) |
| |
| resetFormat() |
| setAlignment(Union[Qt.Alignment, Qt.AliginmentFlag]) |
3.3.2.3 文本操作
| setTextVisible(bool) |
| text() |
| setTextDirection(QProcessBar.Direction) |
3.3.3 信号
3.4 QErrorMessaage
3.4.1 描述
错误消息小部件由文本标签和复选框组成;该复选框允许用户控制将来是否再次显示相同的错误消息
继承自QDialog
3.4.2 功能作用
| |
| |
| |
| from PyQt5.Qt import * |
| import sys |
| |
| app = QApplication(sys.argv) |
| w = QWidget() |
| w.resize(500, 500) |
| |
| qb = QErrorMessage(w) |
| qb.setWindowTitle("错误提示") |
| qb.showMessage("你确定要继续操作吗?") |
| qb.exec() |
| |
| QErrorMessage.qtHandler() |
| qDebug("xxx") |
| qWarning("eee") |
| |
| w.show() |
| sys.exit(app.exec_()) |
3.4.3 信号
继承父类
3.5 QProgressDialog
3.5.1 描述
提供一个缓慢的操作进度反馈,进度对话框用于向用户指示操作符花费多长时间,并演示应用程序尚未冻结;它还可以为用户提供中止操作的机会
继承自 QDialog
3.5.2 功能作用
3.5.2.1 基础
| |
| |
| |
| from PyQt5.Qt import * |
| import sys |
| |
| app = QApplication(sys.argv) |
| w = QWidget() |
| w.resize(500, 500) |
| |
| qpd = QProgressDialog("进度条", "确定", 0, 100, w) |
| |
| |
| qpd.setAutoClose(False) |
| qpd.setAutoReset(False) |
| |
| |
| |
| |
| |
| w.show() |
| sys.exit(app.exec_()) |
3.5.2.2 界面内容编辑
| |
| setWindowTitle(str) |
| |
| |
| setLabelText(str) |
| |
| |
| setCancelButtonText(str) |
| |
| |
| setBar(QProgressBar) |
| setCancelButton(QPushButton) |
| setLabel(QLabel) |
3.5.2.3 数据处理
| |
| setMinimum(int) |
| setMaximum(int) |
| setRange(int min, int max) |
| |
| |
| setValue(int) |
| |
| |
| cancel() |
| wasCanceled() |
3.5.3 信号
3.6 QMessageBox
3.6.1 描述
用于通知用户或请求用户的提问和接收应答的一个模态窗口;无论其展示方式是什么,都为模态窗口
对话框的构成:
继承自 QDialog
3.6.2 功能作用
3.6.2.1 构造函数
| QMessageBox(parent: QWidget = None) |
| QMessageBox(QMessageBox.Icon, str, str, buttons: Union[QMessageBox.StandardButtons, QMessageBox.StandardButton] = QMessageBox.NoButton, parent: QWidget = None, flags: Union[Qt.WindowFlags, Qt.WindowType] = Qt.Dialog|Qt.MSWindowsFixedSizeDialogHint) |
3.6.2.2 内容展示
| |
| setWindowTitle(str) |
| |
| |
| seticon(QMessageBox.Icon) |
| |
| |
| setIconPixmap(QPixmap) |
| |
| |
| setText(str) |
| setTextFormat(Qt.TextFormat) |
| |
| |
| setInformativeText(str) |
| |
| |
| setDetailedText(str) |
| |
| |
| setCheckBox(QCheckBox) |
3.6.2.3 按钮
添加移除按钮
| addButton(QabstractButton, QMessageBoxButtonRole) |
| addButton(str, QMessageBox.ButtonRole) |
| addButton(QMessageBox.StandardButton) |
| removeButton(QAbstractButton) |
设置标准按钮
| setStandardButtons(Union[QMessageBox.StandardButtons, QMessageBox.StandardBox.StandardButton]) |
默认按钮
| setDefaultButton(QPushButton) |
| setDefaultButton(QMessageBox.StandardButton) |
退出按钮
| setEscapeButton(QAbstractButton) |
| setEscapeButton(QMessageBox.StandardButton) |
| |
获取按钮
| buttons() |
| button(QMessageBox.StandardButton) |
按钮角色
| buttonRole(QAbstractButton) |
被点击的按钮
参数请到源码查看
3.6.2.4 文本交互
| setTextInteractionFlags(Qt.TextInteractionFlag) |
| textInteractionFlags() |
相关参数和上文的文本交互类似:【[点我](#3.1.2.2 文本交互)】,其仅仅控制主标题
3.6.2.5 静态方法
| |
| about(QWidget, str, str) |
| |
| aboutQt(QWidget, title) |
| |
| |
| critical(QWidget, str, str, buttons: Union[QMessageBox.StandardButtons, QMessageBox.StandardButton] = QMessageBox.Ok, defaultButton: QMessageBox.StandardButton = QMessageBox.NoButton) -> QMessageBox.StandardButton |
| |
| |
| information(QWidget, str, str, buttons: Union[QMessageBox.StandardButtons, QMessageBox.StandardButton] = QMessageBox.Ok, defaultButton: QMessageBox.StandardButton = QMessageBox.NoButton) -> QMessageBox.StandardButton |
| |
| |
| question(QWidget, str, str, buttons: Union[QMessageBox.StandardButtons, QMessageBox.StandardButton] = QMessageBox.StandardButtons(QMessageBox.Yes|QMessageBox.No), defaultButton: QMessageBox.StandardButton = QMessageBox.NoButton) -> QMessageBox.StandardButton |
| |
| |
| warning(QWidget, str, str, buttons: Union[QMessageBox.StandardButtons, QMessageBox.StandardButton] = QMessageBox.Ok, defaultButton: QMessageBox.StandardButton = QMessageBox.NoButton) -> QMessageBox.StandardButton |
静态方法,快速展出对话框
3.6.3 信号
| buttonClicked(QAbstractButton) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?