我正在尝试做一个基于窗口的程序,我必须在几个不同的QLineEdit框中输入值,并且任何文本更改都会触发计算函数(例如,不同框的总和)。
我已经成功地使用QDoubleValidator限制了数字输入,但我知道如果我输入的不是数字,程序就会崩溃。因此,我提出了关于小数点分隔符的问题,因为我知道将来的用户将尝试只使用.*来创建0.*值
作为一个例子,我做了以下工作:
float_validator = QtGui.QDoubleValidator(self)
self.lineEdit_AC_CLmax.setValidator(float_validator)
CLmax = float(self.lineEdit_AC_CLmax.text())我已经成功地使用数字测试了这个程序,但是每当我输入.时,程序就会崩溃(显然,没有办法让float('.')工作)。
有没有办法一开始限制.的使用,只允许在QLineEdit中的#1数字之后使用?或者,有没有办法将.输入转换成0.
发布于 2019-06-19 00:50:44
对于解决方案,我有几种方法:
-可以对其进行限制,以便第一个数字不是".":
class DoubleValidator(QtGui.QDoubleValidator):
def validate(self, _input, pos):
res = super(DoubleValidator, self).validate(_input, pos)
if _input == "." and pos == 1:
res = (QtGui.QValidator.Invalid, _input, pos)
return res
# ...
validator_a = DoubleValidator(self, notation=QtGui.QDoubleValidator.StandardNotation)
self.le_a = QtWidgets.QLineEdit()
self.le_a.setValidator(validator_a)
# ...但"+“和"-”也应该这样做,因为它们会产生相同的问题,但您不认为这是不合适的吗?例如:如何放置负值?你必须放置至少一个数字,然后将光标移动到开头才能放置标志,这太不舒服了。所以对我来说,这不是一个合理的解决方案。
-在执行操作之前验证文本:
用户不应该有超过必要的限制,在这种情况下,我认为验证即使是浮点数也无效的情况并在计算中建立默认值就足够了,例如"0":
from PyQt5 import QtCore, QtGui, QtWidgets
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
validator_a = QtGui.QDoubleValidator(self, notation=QtGui.QDoubleValidator.StandardNotation)
self.le_a = QtWidgets.QLineEdit(textChanged=self.update_result)
self.le_a.setValidator(validator_a)
validator_b = QtGui.QDoubleValidator(self, notation=QtGui.QDoubleValidator.StandardNotation)
self.le_b = QtWidgets.QLineEdit(textChanged=self.update_result)
self.le_b.setValidator(validator_b)
self.result_label = QtWidgets.QLabel()
lay = QtWidgets.QHBoxLayout(self)
lay.addWidget(self.le_a)
lay.addWidget(QtWidgets.QLabel("+"))
lay.addWidget(self.le_b)
lay.addWidget(QtWidgets.QLabel("="))
lay.addWidget(self.result_label)
self.update_result()
@QtCore.pyqtSlot()
def update_result(self):
a = self.le_a.text()
b = self.le_b.text()
if a in ("", ".", "-", "+"):
a = 0
if b in ("", ".", "-", "+"):
b = 0
res = float(a) + float(b)
self.result_label.setNum(res)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())-不要使用QLineEdit,而要使用QDoubleSpinBox:
尽管QLineEdit允许将字符限制为仅验证数字,但最好使用专门用于从用户获取数值的QDoubleSpinBox:
from PyQt5 import QtCore, QtGui, QtWidgets
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
self.sp_a = QtWidgets.QDoubleSpinBox(valueChanged=self.update_result)
self.sp_b = QtWidgets.QDoubleSpinBox(valueChanged=self.update_result)
self.result_label = QtWidgets.QLabel()
lay = QtWidgets.QHBoxLayout(self)
lay.addWidget(self.sp_a)
lay.addWidget(QtWidgets.QLabel("+"))
lay.addWidget(self.sp_b)
lay.addWidget(QtWidgets.QLabel("="))
lay.addWidget(self.result_label)
self.update_result()
@QtCore.pyqtSlot()
def update_result(self):
a = self.sp_a.value()
b = self.sp_b.value()
res = a + b
self.result_label.setNum(res)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())https://stackoverflow.com/questions/56652959
复制相似问题