大家好,我是小居。
上篇把信号与槽说清楚了,这篇我们来把几个最常用的控件过一遍。
输入框用来收用户文字;
下拉框用来选选项;
复选框和单选框用来做开关设置。
这些控件在几乎每个桌面应用里都会出现。
QLineEdit:单行文本输入
最常用的输入框,用来输入单行文字:
from PyQt5.QtWidgets import QLineEditline = QLineEdit()line.setPlaceholderText("请输入用户名") # 占位提示文字line.setMaxLength(20) # 最大输入20个字符line.setReadOnly(True) # 只读模式,不可编辑
获取和设置内容:
text = line.text() # 读取输入内容line.setText("默认值") # 设置默认文字line.clear() # 清空输入框
常用信号:
line.textChanged.connect(lambda t: print(f"输入变化:{t}")) # 内容变化时line.returnPressed.connect(lambda: print("回车")) # 回车按下时line.editingFinished.connect(lambda: print("编辑结束")) # 失去焦点时
密码输入模式:
password = QLineEdit()password.setEchoMode(QLineEdit.Password) # 暗文显示password.setEchoMode(QLineEdit.PasswordEchoOnEdit) # 编辑时显示,结束后暗文
QTextEdit:多行文本输入
用来输入多行文字,比如留言、笔记:
text_edit = QTextEdit()text_edit.setPlaceholderText("请输入内容...")text_edit.setMinimumHeight(150) # 设置最小高度
获取和设置内容:
content = text_edit.toPlainText() # 获取纯文本content = text_edit.toHtml() # 获取HTML格式text_edit.setText("默认内容")
限制输入字符数:
text_edit.setMaxLength(500)
QComboBox:下拉选择框
点击后展开选项列表,选择其中一个:
combo = QComboBox()combo.addItem("Python") # 添加选项combo.addItem("Java")combo.addItem("JavaScript")combo.addItems(["Go", "Rust"]) # 批量添加combo.setCurrentIndex(0) # 设置默认选中第0项combo.setCurrentText("Python") # 按文字设置选中
获取和监听选中:
selected = combo.currentText() # 获取当前选中文字index = combo.currentIndex() # 获取当前选中索引combo.currentIndexChanged.connect(lambda i: print(f"选中索引:{i}"))combo.currentTextChanged.connect(lambda t: print(f"选中文字:{t}"))
QCheckBox:复选框(可多选)
勾选框,可以同时勾选多个:
cb1 = QCheckBox("记住我")cb2 = QCheckBox("自动登录")# 设置初始状态cb1.setChecked(True) # 默认勾选cb2.setChecked(False) # 默认不勾选# 监听状态变化cb1.stateChanged.connect(lambda state: print(f"状态:{state}"))# state = 0 未勾选,state = 2 勾选
QRadioButton:单选按钮
一组里只能选一个:
male = QRadioButton("男")female = QRadioButton("女")male.setChecked(True) # 默认选中男# 放在同一个父控件下,自动互斥male.toggled.connect(lambda checked: print(f"男{'选中'if checked else'取消'}")female.toggled.connect(lambda checked: print(f"女{'选中'if checked else'取消'}")
完整示例:注册表单
把上面的控件串起来,写一个注册表单界面:
import sysfrom PyQt5.QtWidgets import ( QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QGridLayout, QLabel, QLineEdit, QComboBox, QCheckBox, QPushButton, QMessageBox, QRadioButton)app = QApplication(sys.argv)window = QMainWindow()window.setWindowTitle("用户注册")window.resize(400, 320)central = QWidget()window.setCentralWidget(central)main_layout = QVBoxLayout()central.setLayout(main_layout)# 用户名main_layout.addWidget(QLabel("用户名"))username = QLineEdit()username.setPlaceholderText("请输入用户名")main_layout.addWidget(username)# 密码main_layout.addWidget(QLabel("密码"))password = QLineEdit()password.setEchoMode(QLineEdit.Password)password.setPlaceholderText("请输入密码")main_layout.addWidget(password)# 性别gender_layout = QHBoxLayout()gender_layout.addWidget(QLabel("性别"))gender_layout.addWidget(QRadioButton("男"))gender_layout.addWidget(QRadioButton("女"))gender_layout.addStretch()main_layout.addLayout(gender_layout)# 协议agree = QCheckBox("我已阅读并同意用户协议")main_layout.addWidget(agree)# 提交按钮submit_btn = QPushButton("提交注册")main_layout.addWidget(submit_btn)def submit():ifnot agree.isChecked(): QMessageBox.warning(window, "提示", "请勾选同意协议")return QMessageBox.information(window, "成功", f"注册成功!\n用户名:{username.text()}" )submit_btn.clicked.connect(submit)window.show()sys.exit(app.exec_())

逐个控件对应填入,最后点提交按钮弹窗显示填写结果。这就是常用控件的基本用法。
下篇说说弹窗,对话框和消息提示怎么用。
喜欢小居的文章,点赞、关注、转发,点个“在看”不迷路~
有问题找小居,小居看到马上回!
源码获取:公众号回复「04PyQt5」即可