CoolFace
Apppublic

Aluode/PerceptionLabPortable

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
SpinBox.py133 linesDownload Raw Back to examples
1"""2This example demonstrates the SpinBox widget, which is an extension of 3QDoubleSpinBox providing some advanced features:4 5  * SI-prefixed units6  * Non-linear stepping modes7  * Bounded/unbounded values8 9"""10 11import ast12 13import pyqtgraph as pg14from pyqtgraph.Qt import QtWidgets15 16app = pg.mkQApp("SpinBox Example")17 18 19spins = [20    ("Floating-point spin box, min=0, no maximum.<br>Non-finite values (nan, inf) are permitted.",21     pg.SpinBox(value=5.0, bounds=[0, None], finite=False)),22    ("Integer spin box, dec stepping<br>(1-9, 10-90, 100-900, etc), decimals=4", 23     pg.SpinBox(value=10, int=True, dec=True, minStep=1, step=1, decimals=4)),24    ("Float with SI-prefixed units<br>(n, u, m, k, M, etc)", 25     pg.SpinBox(value=0.9, suffix='V', siPrefix=True)),26    ("Float with SI-prefixed units,<br>dec step=0.1, minStep=0.1", 27     pg.SpinBox(value=1.0, suffix='PSI', siPrefix=True, dec=True, step=0.1, minStep=0.1)),28    ("Float with SI-prefixed units,<br>dec step=0.5, minStep=0.01", 29     pg.SpinBox(value=1.0, suffix='V', siPrefix=True, dec=True, step=0.5, minStep=0.01)),30    ("Float with SI-prefixed units,<br>dec step=1.0, minStep=0.001", 31     pg.SpinBox(value=1.0, suffix='V', siPrefix=True, dec=True, step=1.0, minStep=0.001)),32    ("Float with SI-prefixed units,<br>scaleAtZero=1e-6, step=1e-9",33     pg.SpinBox(value=0, suffix='V', siPrefix=True, scaleAtZero=1e-6, step=1e-9)),34    ("Float with SI prefix but no suffix",35     pg.SpinBox(value=1e9, siPrefix=True)),36    ("Float with custom formatting", 37     pg.SpinBox(value=23.07, format='${value:0.02f}',38                regex = r'\$?(?P<number>(-?\d+(\.\d+)?)|(-?\.\d+))$')),39    ("Int with suffix",40     pg.SpinBox(value=999, step=1, int=True, suffix="V")),41    ("Int with custom formatting", 42     pg.SpinBox(value=4567, step=1, int=True, bounds=[0,None], format='0x{value:X}', 43                regex='(0x)?(?P<number>[0-9a-fA-F]+)$',44                evalFunc=lambda s: ast.literal_eval('0x'+s))),45    ("Integer with bounds=[10, 20] and wrapping",46     pg.SpinBox(value=10, bounds=[10, 20], int=True, minStep=1, step=1, wrapping=True)),47]48 49 50win = QtWidgets.QMainWindow()51win.setWindowTitle('pyqtgraph example: SpinBox')52cw = QtWidgets.QWidget()53layout = QtWidgets.QGridLayout()54cw.setLayout(layout)55win.setCentralWidget(cw)56win.show()57#win.resize(300, 600)58changingLabel = QtWidgets.QLabel()  ## updated immediately59changedLabel = QtWidgets.QLabel()   ## updated only when editing is finished or mouse wheel has stopped for 0.3sec60changingLabel.setMinimumWidth(200)61font = changingLabel.font()62font.setBold(True)63font.setPointSize(14)64changingLabel.setFont(font)65changedLabel.setFont(font)66labels = []67 68 69def valueChanged(sb):70    changedLabel.setText("Final value: %s" % str(sb.value()))71 72def valueChanging(sb, value):73    changingLabel.setText("Value changing: %s" % str(sb.value()))74 75    76for text, spin in spins:77    label = QtWidgets.QLabel(text)78    labels.append(label)79    layout.addWidget(label)80    layout.addWidget(spin)81    spin.sigValueChanged.connect(valueChanged)82    spin.sigValueChanging.connect(valueChanging)83 84layout.addWidget(changingLabel, 0, 1)85layout.addWidget(changedLabel, 2, 1)86 87 88#def mkWin():89    #win = QtWidgets.QMainWindow()90    #g = QtWidgets.QFormLayout()91    #w = QtWidgets.QWidget()92    #w.setLayout(g)93    #win.setCentralWidget(w)94    #s1 = SpinBox(value=5, step=0.1, bounds=[-1.5, None], suffix='units')95    #t1 = QtWidgets.QLineEdit()96    #g.addRow(s1, t1)97    #s2 = SpinBox(value=10e-6, dec=True, step=0.1, minStep=1e-6, suffix='A', siPrefix=True)98    #t2 = QtWidgets.QLineEdit()99    #g.addRow(s2, t2)100    #s3 = SpinBox(value=1000, dec=True, step=0.5, minStep=1e-6, bounds=[1, 1e9], suffix='Hz', siPrefix=True)101    #t3 = QtWidgets.QLineEdit()102    #g.addRow(s3, t3)103    #s4 = SpinBox(int=True, dec=True, step=1, minStep=1, bounds=[-10, 1000])104    #t4 = QtWidgets.QLineEdit()105    #g.addRow(s4, t4)106 107    #win.show()108 109    #import sys110    #for sb in [s1, s2, s3,s4]:111 112        ##QtCore.QObject.connect(sb, QtCore.SIGNAL('valueChanged(double)'), lambda v: sys.stdout.write(str(sb) + " valueChanged\n"))113        ##QtCore.QObject.connect(sb, QtCore.SIGNAL('editingFinished()'), lambda: sys.stdout.write(str(sb) + " editingFinished\n"))114        #sb.sigValueChanged.connect(valueChanged)115        #sb.sigValueChanging.connect(valueChanging)116        #sb.editingFinished.connect(lambda: sys.stdout.write(str(sb) + " editingFinished\n"))117    #return win, w, [s1, s2, s3, s4]118#a = mkWin()119 120 121#def test(n=100):122    #for i in range(n):123        #win, w, sb = mkWin()124        #for s in sb:125            #w.setParent(None)126            #s.setParent(None)127            #s.valueChanged.disconnect()128            #s.editingFinished.disconnect()129 130 131if __name__ == '__main__':132    pg.exec()133