lineedit显示数字
时间: 2024-05-09 09:19:21 浏览: 70
两个lineedit设置焦点和数字输入
4星 · 用户满意度95%
可以通过设置lineEdit的inputMask属性来限制只能输入数字,例如:
```python
from PyQt5.QtWidgets import QLineEdit, QWidget, QVBoxLayout, QApplication
app = QApplication([])
window = QWidget()
layout = QVBoxLayout()
lineEdit = QLineEdit()
lineEdit.setInputMask("9999") # 只能输入四位数字
layout.addWidget(lineEdit)
window.setLayout(layout)
window.show()
app.exec_()
```
也可以使用正则表达式来限制输入,例如:
```python
from PyQt5.QtWidgets import QLineEdit, QWidget, QVBoxLayout, QApplication
from PyQt5.QtCore import QRegExp
from PyQt5.QtGui import QRegExpValidator
app = QApplication([])
window = QWidget()
layout = QVBoxLayout()
lineEdit = QLineEdit()
regExp = QRegExp("[0-9]*") # 只能输入数字
validator = QRegExpValidator(regExp)
lineEdit.setValidator(validator)
layout.addWidget(lineEdit)
window.setLayout(layout)
window.show()
app.exec_()
```
阅读全文