Exemplo n.º 1
0
    def update_progress_bar(new, old):
        fraction = (new.magnitude - start.value()) / (stop.value() - start.value())
        progress.setValue(fraction * 100)

    inst.frequency_changed.connect(update_progress_bar)

    # <--------- New code--------->
    # Define a function to read the values from the widget and call scan_frequency
    class Scanner(QtCore.QObject):

        def scan(self):
            # Call the scan frequency
            scan_frequency(inst, start.value() * Hz, stop.value() * Hz,
                           step.value() * Hz, wait.value() * sec)
            # When it finishes, set the progress to 100%
            progress.setValue(100)

    thread = QtCore.QThread()
    scanner = Scanner()
    scanner.moveToThread(thread)
    thread.start()

    # Connect the clicked signal of the scan button to the function
    scan.clicked.connect(scanner.scan)

    qapp.aboutToQuit.connect(thread.quit)
    # <--------- End of new code --------->

    main.show()
    exit(qapp.exec_())
Exemplo n.º 2
0
def initialize_and_report(widget,
                          drivers,
                          register_finalizer=True,
                          initializing_msg='Initializing ...',
                          initialized_msg='Initialized',
                          concurrent=True,
                          dependencies=None):
    """Initialize drivers while reporting the status in a QtWidget.

    :param widget: Qt Widget where the status information is going to be shown.
    :param drivers: iterable of drivers to initialize.
    :param register_finalizer: register driver.finalize method to be called at python exit.
    :param initializing_msg: message to be displayed while initializing.
    :param initialized_msg: message to be displayed after successful initialization.
    :param concurrent: indicates that drivers with satisfied dependencies
                       should be initialized concurrently.
    :param dependencies: indicates which drivers depend on others to be initialized.
                         each key is a driver name, and the corresponding
                         value is an iterable with its dependencies.
    :return: the QThread doing the initialization.
    """
    timing = {}

    thread = QtCore.QThread()
    helper = InitializerHelper(drivers, register_finalizer, concurrent,
                               dependencies)
    helper.moveToThread(thread)
    thread.helper = helper

    if isinstance(widget, QtGui.QTableWidget):

        def _initializing(driver):
            timing[driver] = time.time()
            row = drivers.index(driver)
            widget.setItem(row, 2, QtGui.QTableWidgetItem(initializing_msg))

        def _initialized(driver):
            delta = time.time() - timing[driver]
            row = drivers.index(driver)
            widget.setItem(
                row, 2,
                QtGui.QTableWidgetItem(initialized_msg +
                                       ' ({:.1f} sec)'.format(delta)))

        def _exception(driver, e):
            delta = time.time() - timing[driver]
            row = drivers.index(driver)
            widget.setItem(
                row, 2,
                QtGui.QTableWidgetItem('{} ({:.1f} sec)'.format(e, delta)))

        def _done(duration):
            widget.setItem(
                len(drivers), 2,
                QtGui.QTableWidgetItem('{:.1f} sec'.format(duration)))
            thread.quit()

        widget.clearContents()
        widget.setRowCount(len(drivers) + 1)
        for row, driver in enumerate(drivers):
            widget.setItem(row, 0, QtGui.QTableWidgetItem(driver.name))
            widget.setItem(row, 1,
                           QtGui.QTableWidgetItem(driver.__class__.__name__))
            widget.setItem(row, 2, QtGui.QTableWidgetItem(''))

        widget.resizeColumnToContents(0)
        widget.horizontalHeader().setStretchLastSection(True)

    elif isinstance(widget, QtGui.QLineEdit):

        def _initializing(driver):
            timing[driver] = time.time()
            widget.setText('{} ({}) > {}'.format(driver.name,
                                                 driver.__class__.__name__,
                                                 initializing_msg))

        def _initialized(driver):
            delta = time.time() - timing[driver]
            widget.setText('{} ({}) > {} ({:.1f} sec)'.format(
                driver.name, driver.__class__.__name__, initialized_msg,
                delta))

        def _exception(driver, e):
            delta = time.time() - timing[driver]
            widget.setText('{} ({}) > {} ({:.1f} sec)'.format(
                driver.name, driver.__class__.__name__, e, delta))

        def _done(duration):
            widget.setText('Initialized in {:.1f} sec'.format(duration))
            thread.quit()

        widget.setReadOnly(True)

    elif isinstance(widget, QtGui.QTextEdit):

        def _initializing(driver):
            timing[driver] = time.time()
            widget.append('{} ({}) > {}'.format(driver.name,
                                                driver.__class__.__name__,
                                                initializing_msg))

        def _initialized(driver):
            delta = time.time() - timing[driver]
            widget.append('{} ({}) > {} ({:.1f} sec)'.format(
                driver.name, driver.__class__.__name__, initialized_msg,
                delta))

        def _exception(driver, e):
            delta = time.time() - timing[driver]
            widget.append('{} ({}) > {} ({:.1f} sec)'.format(
                driver.name, driver.__class__.__name__, e, delta))

        def _done(duration):
            widget.append('Initialized in {:.1f} sec'.format(duration))
            thread.quit()

        widget.setReadOnly(True)

    else:
        raise TypeError('Unknown widget type {}.'.format(type(widget)))

    thread.started.connect(helper.process)
    helper.initializing.connect(_initializing)
    helper.initialized.connect(_initialized)
    helper.exception.connect(_exception)
    helper.finished.connect(_done)

    thread.start()
    return thread