【问题标题】:How to user input an infinity value to a QDoubleSpinBox?如何用户向 QDoubleSpinBox 输入无穷大值?
【发布时间】:2019-08-07 12:58:20
【问题描述】:

我正在尝试使用 PyQt5 在 Python 3.7 中设置一个 QDoubleSpinBox,它可以采用从 -np.inf 到 np.inf 的一系列值。我还希望用户将值设置为其中一个,-np.inf 或 np.inf。我/用户将如何做到这一点?

调整QDoubleSpinBox的最小值和最大值后,可以在代码中设置值,“-inf”或“inf”出现在显示的框中。

from PyQt5.QtWidgets import QDoubleSpinBox

[...]

dsbTest = QDoubleSpinBox()

# first set the desired range 
dsbTest.setRange(-np.inf, np.inf)

# set the value to positive infinity, successfully
dsbTest.setValue(np.inf)

但是,在将其更改为任何其他值(比如 5)后,我发现自己无法将“-inf”或“inf”重新输入 GUI。

当我输入“i”或“inf”时,没有输入,我的意思是,显示的当前值不会从5改变。

【问题讨论】:

  • 您确实明白,虽然您可以声明这一点,但如果没有一些复杂的处理,您实际上无法实现这一点,因为您确实对数字的大小有限制。此外,这是相当不切实际的,因为在输入数字时没有人真的需要去任何接近无穷大的地方,只要让它们变得非常大,你就可以了。
  • 至于尝试在数字字段中输入文本,您必须抓住按键事件,如果是“i”,则通过代码输入“inf”或“-inf”值。
  • 旋转双打不像旋转有序和离散集合那么有趣。在这种情况下,您不能只使用带有验证器的 QLineEdit 吗?
  • 一种解决方法可能是提供两个额外的按钮来设置-np.infnp.inf。随着相应。 Unicode 点 (U+221E),您甚至可以给它们贴上漂亮的标签,例如 -∞+∞... ;-)
  • @Scheff 通过覆盖 keyPressEvent 并捕获“Home”和“End”按钮来分配最小值和最大值来解决它。我认为这很直观,因为箭头键和向上翻页和向下翻页可用于增量更改值。但是,我将显示一个工具提示以寻求帮助。非常感谢您的帮助!

标签: python python-3.x pyqt pyqt5


【解决方案1】:

我最终这样做了,创建了一个子类。

class InftyDoubleSpinBox(QDoubleSpinBox):

    def __init__(self):
        super(QDoubleSpinBox, self).__init__()

        self.setMinimum(-np.inf)
        self.setMaximum(np.inf)

    def keyPressEvent(self, e: QtGui.QKeyEvent):

        if e.key() == QtCore.Qt.Key_Home:
            self.setValue(self.maximum())
        elif e.key() == QtCore.Qt.Key_End:
            self.setValue(self.minimum())
        else:
            super(QDoubleSpinBox, self).keyPressEvent(e)

我在开始时将最小值和最大值设置为-np.inf,np.inf。在 keyPressEvent 中会捕捉 Home 和 End 按钮,将值设置为最小值或最大值。

对于任何其他键,QDoubleSpinBox 将像往常一样做出反应,因为调用了基本函数。

这也适用于调用 init() 后的其他分配的最小值/最大值。这对我来说是可取的。

【讨论】:

    【解决方案2】:

    float("inf")float("INF")float("Inf")float("inF")float("infinity") 创建一个float 对象持有infinity

    float("-inf")float("-INF")float("-Inf")float("-infinity") 创建一个持有 负无穷大

    的浮点对象

    我们可以将-infinity设置为最小值,将+infinity设置为最大值QDoubleSpinBox

    box = QDoubleSpinBox()
    box.setMinimum(float("-inf"))
    box.setMaximum(float("inf"))
    

    来源:

    import sys
    
    from PySide6.QtWidgets import QMainWindow, QApplication, QDoubleSpinBox, QWidget, QFormLayout, QLineEdit, QPushButton
    
    
    def getDoubleSpinBox() -> QDoubleSpinBox:
        box = QDoubleSpinBox()
        box.setPrefix("₹")
        box.setMinimum(float("-inf"))
        box.setMaximum(float("inf"))
        box.setSingleStep(0.05)
        box.setValue(25_000)
        return box
    
    
    def getLineEdit(placehoder: str, password: bool = False):
        lineEdit = QLineEdit()
        lineEdit.setPlaceholderText(placehoder)
        if password:
            lineEdit.setEchoMode(QLineEdit.Password)
        return lineEdit
    
    
    class MainWindow(QMainWindow):
        def __init__(self):
            super().__init__()
            self.initUI()
    
        def initUI(self):
            self.setWindowTitle("Open Bank")
            self.widget = QWidget()
            self.widgetLayout = QFormLayout()
            self.widgetLayout.addRow("ID", getLineEdit(placehoder="Investor name"))
            self.widgetLayout.addRow("Investment", getDoubleSpinBox())
            self.widgetLayout.addRow("Password", getLineEdit(placehoder="Enter secret password", password=True))
            self.widgetLayout.addRow(QPushButton("Invest"), QPushButton("Cancel"))
            self.widget.setLayout(self.widgetLayout)
            self.setCentralWidget(self.widget)
    
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        win = MainWindow()
        win.show()
        sys.exit(app.exec())
    

    窗口:

    【讨论】:

      猜你喜欢
      • 2016-09-22
      • 1970-01-01
      • 2019-10-11
      • 2011-02-24
      • 2022-08-12
      • 2020-07-19
      • 2019-01-23
      • 2012-07-10
      相关资源
      最近更新 更多