QEDITLINE 设置只能输入数字
时间: 2024-05-28 10:05:05 浏览: 19
可以使用QRegExpValidator来限制QLineEdit只能输入数字,具体实现方法如下:
```python
from PyQt5.QtCore import QRegExp
from PyQt5.QtGui import QRegExpValidator
from PyQt5.QtWidgets import QApplication, QLineEdit, QWidget, QVBoxLayout
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('QLineEdit设置只能输入数字')
self.setGeometry(400, 400, 400, 200)
vbox = QVBoxLayout()
lineEdit = QLineEdit()
lineEdit.setPlaceholderText('请输入数字')
reg = QRegExp("[0-9]+$")
validator = QRegExpValidator(reg)
lineEdit.setValidator(validator)
vbox.addWidget(lineEdit)
self.setLayout(vbox)
if __name__ == '__main__':
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()
```
运行程序后,QLineEdit就只能输入数字了。
相关推荐
![text/plain](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)