示例#1
0
def test_basic_math(qtbot):
    widget = ExprEdit(text='2*cos(pi) + 20/5')
    show(locals())
    assert widget.getSymbols() == dict()
    assert widget.getExpr() == 2
    assert widget.getValue() == 2

    widget = ExprEdit(text='2*cos(pi) + pi')
    show(locals())
    assert widget.getSymbols() == dict()
    assert widget.getExpr() == parseExpr('-2 + pi')
    assert widget.getExpr() != parseExpr('-2 + pi').evalf()
    assert widget.getValue() == parseExpr('-2 + pi').evalf()
layout = QVBoxLayout()

sym = SymbolEdit(text='symbol_name')
sym.hasError[object].connect(printer('SymbolEdit.hasError[object]'))
sym.exprChanged[object].connect(
    printer('SymbolEdit.exprChanged[object] -> Symbol is:'))
sym.setToolTip("Enter a symbol name")
layout.addWidget(sym)

expr = ExprEdit(text='1*a + 2*b - 1*a + cos(pi)')
expr.hasError[str].connect(printer('ExprEdit.hasError[str]'))
expr.exprChanged[str].connect(
    printer('ExprEdit.exprChanged[str] -> Expression is:'))
expr.editingFinished.connect(lambda: show_symbols(expr))
expr.editingFinished.connect(
    lambda: print('ExprEdit.editingFinished -> getValue():', expr.getValue()))
expr.setToolTip("Do some math")
layout.addWidget(expr)

unit = UnitEdit(text='2*mm + 1*inch')
unit.hasError[str].connect(printer('UnitEdit.hasError[str]'))
unit.exprChanged[object].connect(printer('UnitEdit.exprChanged[object]'))
unit.exprChanged.connect(
    lambda: print('UnitEdit.exprChanged -> getExpr()', unit.getExpr()))
unit.exprChanged.connect(
    lambda: print('UnitEdit.exprChanged -> getValue()', unit.getValue()))
unit.valueChanged[object].connect(printer('UnitEdit.valueChanged[object]'))
unit.setToolTip("Do math with units")
layout.addWidget(unit)

dim = DimensionEdit(text='2*mm + 1*inch', dimension='length')