Python PyQt5 QLineEdit 限制輸入數字

本篇 ShengYu 介紹 Python PyQt5 QLineEdit 限制輸入數字的方法。

PyQt5 QLineEdit 用 QIntValidator 限制輸入數字

這邊就以簡單的數學問題回答為例,需要限制使用者只能在 QLineEdit 裡輸入數字,
PyQt5 QLineEdit 限制輸入數字的方式可以使用 QLineEdit.setValidator() 的方式驗證文字的類別,例如下例中的也就是下例中的 QIntValidator 就是專門驗證是不是整數的驗證器,範例如下,

python-pyqt-qlineedit-number-only.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QGridLayout, QLineEdit,
QLabel, QPushButton, QMessageBox)
from PyQt5.QtGui import QIntValidator

class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.initUI()

def initUI(self):
self.setWindowTitle('my window')
self.setGeometry(50, 50, 240, 60)

gridlayout = QGridLayout()
self.setLayout(gridlayout)

self.mylabel = QLabel('1+1=', self)
gridlayout.addWidget(self.mylabel, 0, 0)

self.mylineedit = QLineEdit(self)
gridlayout.addWidget(self.mylineedit, 0, 1)
self.mylineedit.setValidator(QIntValidator()) # only int

self.mybutton = QPushButton('完成', self)
gridlayout.addWidget(self.mybutton, 1, 0, 1, 2)
self.mybutton.clicked.connect(self.onButtonClick)

def onButtonClick(self):
# print(self.mylineedit.text())
if self.mylineedit.text() == '':
QMessageBox.about(self, 'message', '未輸入答案')
elif self.mylineedit.text() == '2':
QMessageBox.about(self, 'message', '答對了')
else:
QMessageBox.about(self, 'message', '答錯了')

if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyWidget()
w.show()
sys.exit(app.exec_())

結果圖如下,實際操作可以發現只能輸入數字,無法輸入數字以外的文字,

QIntValidator() 還能限制你輸入的整數範圍,例如 0~255,改寫成這樣即可,

1
self.mylineedit.setValidator(QIntValidator(0, 255)) # 0~255

以及限制使用者只能輸入 double 數字,改寫成這樣即可,

1
2
3
from PyQt5.QtGui import QDoubleValidator
# ...
self.mylineedit.setValidator(QDoubleValidator()) # only double

PyQt5 QLineEdit 用 QRegExpValidator 正規表達式來限制輸入數字

這邊我們使用 QRegExpValidator 搭配正規標達式來作數字的驗證器,在這裡你也可以客製化一些其它規則,本範例先示範限制數字,

python-pyqt-qlineedit-number-only2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QGridLayout, QLineEdit,
QLabel, QPushButton, QMessageBox)
from PyQt5.QtGui import QRegExpValidator
from PyQt5.QtCore import QRegExp

class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.initUI()

def initUI(self):
self.setWindowTitle('my window')
self.setGeometry(50, 50, 240, 60)

gridlayout = QGridLayout()
self.setLayout(gridlayout)

self.mylabel = QLabel('1+1=', self)
gridlayout.addWidget(self.mylabel, 0, 0)

self.mylineedit = QLineEdit(self)
gridlayout.addWidget(self.mylineedit, 0, 1)
myregexp = QRegExp('[0-9]+')
myvalidator = QRegExpValidator(myregexp, self.mylineedit)
self.mylineedit.setValidator(myvalidator)

self.mybutton = QPushButton('完成', self)
gridlayout.addWidget(self.mybutton, 1, 0, 1, 2)
self.mybutton.clicked.connect(self.onButtonClick)

def onButtonClick(self):
# print(self.mylineedit.text())
if self.mylineedit.text() == '':
QMessageBox.about(self, 'message', '未輸入答案')
elif self.mylineedit.text() == '2':
QMessageBox.about(self, 'message', '答對了')
else:
QMessageBox.about(self, 'message', '答錯了')

if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyWidget()
w.show()
sys.exit(app.exec_())

其它相關文章推薦
Python PyQt5 QLineEdit 文字框用法與範例
Python 新手入門教學懶人包
Python PyQt5 新手入門教學