msl.qt.loop_until_abort module¶
Repeatedly perform a task until aborted by the user.
- class msl.qt.loop_until_abort.LoopUntilAbort(*, loop_delay=0, max_iterations=None, single_shot=False, title=None, bg_color='#DFDFDF', text_color='#20548B', font_family='Helvetica', font_size=14)[source]¶
Bases:
object
Repeatedly perform a task until aborted by the user.
This class provides an interface to show the status of a task (e.g., read a sensor value and write the value to a file) that you want to perform for an unknown period of time (e.g., during lunch or overnight) and you want to stop the task whenever you return. It can be regarded as a way to tell your program to “get as much data as possible until I get back”.
The following example illustrates how to repeatedly write data to a file in a loop:
""" Example script to repeatedly write data to a file until aborted by the user. """ import tempfile from msl.qt import LoopUntilAbort from msl.qt import prompt class LoopExample(LoopUntilAbort): def __init__(self): """Initialize the LoopUntilAbort class and create a file to write data to. Use a 250 ms delay between successive calls to the `loop` method. """ super(LoopExample, self).__init__(loop_delay=250) self.output_path = tempfile.gettempdir() + '/msl-qt-loop-until-abort.txt' self.f = open(self.output_path, mode='wt') self.f.write(f'Started at {self.current_time}\n') def loop(self): """Overrides LoopUntilAbort.loop() This method gets called repeatedly in a loop (every `loop_delay` ms). """ self.f.write(f'Iteration: {self.iteration}\n') self.f.write(f'Elapsed time: {self.elapsed_time}\n') self.set_label_text(f'The current time is\n{self.current_time}') def cleanup(self): """Overrides LoopUntilAbort.cleanup() This method gets called when the LoopExample window is closing. """ self.f.write(f'Stopped at {self.current_time}\n') self.f.close() prompt.information(f'The data was save to\n{self.output_path}\n\n' f'... in case you want to look at it') def main(): loop = LoopExample() loop.start() if __name__ == '__main__': main()
Examples
To run the above example enter the following:
>>> from msl.examples.qt import LoopExample >>> loop = LoopExample() >>> loop.start()
Another example which uses single-shot mode:
>>> from msl.examples.qt import LoopExampleSleep >>> loop = LoopExampleSleep() >>> loop.start()
- Parameters:
loop_delay (
int
, optional) – The delay time, in milliseconds, to wait between successive calls to theloop()
method. For example, if loop_delay =0
then there is no time delay between successive calls to theloop()
method; if loop_delay =1000
then wait 1 second between successive calls to theloop()
method.max_iterations (
int
, optional) – The maximum number of times to call theloop()
method. The default value isNone
, which means to loop until the user aborts the program.single_shot (
bool
, optional) – Whether to call theloop()
method once (and only once). If you specify the value to beTrue
then you must call theloop_once()
method in the subclass whenever you want to run theloop()
one more time. This is useful if theloop()
depends on external factors (e.g., waiting for an oscilloscope to download a trace after a trigger event) and the amount of time that theloop()
requires to complete is not known.title (
str
, optional) – The text to display in the title bar of theQtWidgets.QMainWindow
. IfNone
then uses the name of the subclass as the title.bg_color (
QtGui.QColor
, optional) – The background color of theQtWidgets.QMainWindow
. Can be any data type and value that the constructor of aQtGui.QColor
accepts.text_color (
QtGui.QColor
, optional) – The color of the Elapsed time and Iteration text.Can be any data type and value that the constructor of aQtGui.QColor
accepts.font_family (
QtGui.QFont
, optional) – The font family to use for the text. Can be any value that the constructor of aQtGui.QFont
accepts.font_size (
int
, optional) – The font size of the text.
- property current_time¶
The current time.
- Type:
- property main_window¶
The reference to the main window.
- Type:
- property user_label¶
The reference to the label object that the user can modify the text of.
See also
set_label_text()
To set the text of the
QtWidgets.QLabel
.
- Type:
- cleanup()[source]¶
This method gets called when the
QtWidgets.QMainWindow
is closing.You can override this method to properly clean up any tasks. For example, to close a file that is open.
- loop_once()[source]¶
Run the
loop()
method once.This method should be called if the
LoopUntilAbort
class was initialized with single_shot =True
, in order to run theloop()
method one more time.
- set_label_text(text)[source]¶
Update the text of the label that the user has access to.
- Parameters:
text (
str
) – The text to display in the user-accessible label.
See also
user_label()
For the reference to the
QtWidgets.QLabel
object.
- set_status_bar_text(text)[source]¶
Set the text to display in the status bar of the
QtWidgets.QMainWindow
.- Parameters:
text (
str
) – The text to display in the status bar of theQtWidgets.QMainWindow
.
- start()[source]¶
Show the
QtWidgets.QMainWindow
and start looping.