首页 > 解决方案 > 我如何转换 . 在 PyQt5 的 QLineEdit 中输入 0。

问题描述

我正在尝试制作一个基于窗口的程序,我必须将值输入到几个不同的 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.

标签: pythonpyqtpyqt5

解决方案


对于解决方案,我有几种方法:

- 可以限制第一个数字不是“.”:

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_())

推荐阅读