Cheat Sheet (Copy & Paste)¶
This page contains a collection of useful templates and code snippets. You can use the ‘Copy’ button to copy and paste the code snipped into your script.

Important Facts:
Narda Script Launcher requires Python = 3.7
The Narda Script Launcher API uses four spaces per indentation. The type of indentation must not be changed within a script!
Table of Contents
Note
Narda provides links to offers from third parties in this documentation. The providers of these pages are exclusively responsible for the contents and in particular any damages that may arise from the use or non-use of the information provided in this way. Narda only checks the pages at the time the link is created. All subsequent changes are the responsibility of the provider.
Script Templates¶
There are several template files available that can be used to write a new script.
The simple script template tmpusrscript.py is recommended for scripts with a short execution time of a few seconds.
If a measurement script will need a considerable amount of time ( > 3s) for execution, it is recommended to use the template tmpusrscriptmthread.py. The main difference with the simple template is that it utilizes multithreading to keep the graphical user interface ‘alive’.
The template tmpspacer.py can be used to add a section spacer to a script list.
General Purpose Code Snippets¶
Collection of general purpose code snippets.
Load Value from Config File¶
Loads a value from the config file. First parameter is the value name. Second parameter is the default value.
1 | my_value = self.config.get('my_value', 10)
|
Save Value to Config File¶
Saves a value to the config file. Adds the parameter to the config file, if it does not alreday contain it.
1 | self.config['my_value'] = new_value
|
Load Text from File¶
See also: https://docs.python.org/3.7/tutorial/inputoutput.html –> ‘Reading and Writing Files’
1 2 | with open('somefile.txt', 'r') as myfile:
data = myfile.read()
|
Save Text to File¶
See also: https://docs.python.org/3.7/tutorial/inputoutput.html –> ‘Reading and Writing Files’
‘w’: write/overwrite
‘a’: append
‘r+’: read/write
1 2 | with open('somefile.txt', 'w') as myfile:
myfile.write('My text line\r\n')
|
Save/Load dict to/from json file¶
For this example you have to add the following import statements to the import section of your module:
1 2 | from pathlib import Path
import nardascripting.base.toolbox as tb
|
Save dict to json file¶
1 2 3 4 5 | my_dict = {
'Item1': 1,
'Item2': 2
}
tb.save_dict_to_file(file_path=Path('my_file.json'), dictionary=my_dict)
|
Load dict from json file¶
1 | my_dict = tb.load_dict_from_file(file_path=Path('my_file.json'))
|
Value/text conversion with unit and unit prefix¶
For this example you have to add the following import statement to the import section of your module:
1 | import nardascripting.base.toolbox as tb
|
Convert numeric value to text string with unit and unit prefix¶
1 | num_str = tb.value_to_unit_string(94.7e6, unit='Hz', add_prefix=True, max_dec_places=6)
|
Convert text string with unit and unit prefix to numeric value¶
1 | num_val = tb.unit_string_to_value(value_str='94.7 MHz', unit='Hz', contains_prefix=True)
|
Try to convert string to value of a given type¶
For this example you have to add the following import statements to the import section of your module:
1 | import nardascripting.base.toolbox as tb
|
1 | value = tb.try_str_to_value(value_string='25.4', target_type=float)
|
Check If Task Already Exists¶
Check if a Task with the given name already exists and add a new one if not.
1 2 3 | if not self.signalshark.scpi.check_add_task(TaskTypes.RT_SPECTRUM, 'MyRTSpectrum'):
# Configure new added Task:
self.signalshark.scpi.view.add(1, ViewDirections.BELOW, ViewTypes.SPECTROGRAM)
|
Check If View Already Exists¶
Check if View (e.g. Peak Table) already exists and add one if not.
1 2 3 4 5 6 7 8 9 10 | exists, view_index = self.signalshark.scpi.check_add_view(ViewTypes.PEAK_TABLE)
if view_index < 0:
self.main_gui.MessageBoxModal('Adding peak table not possible.',
'Peak Table', wx.OK | wx.ICON_INFORMATION)
return
if not exists:
# Configure new added peak table:
self.signalshark.scpi.display.set_peaktable_sort(PeakSort.FREQUENCY)
self.signalshark.scpi.marker.set_spectrum_search_peak_excursion(3.0)
|
Connect to SignalShark, change settings and request a value.¶
For a detailed description of all supportet SCPI commands see: SCPI Command Reference.
SignalShark SCPI example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | # Connect to device
if not self.signalshark.connect():
self.MessageBoxModal('Cannot connect to the device!', 'Connection error',
wx.OK | wx.ICON_ERROR)
return
# Add a RT Spectrum Task if it does not already exist.
if not self.signalshark.scpi.check_add_task(TaskTypes.RT_SPECTRUM, 'MyRTSpectrum'):
# Configure new added Task:
self.signalshark.scpi.view.add(1, ViewDirections.BELOW, ViewTypes.SPECTROGRAM)
# Set fstop and fstart:
self.signalshark.scpi.spectrum.set_frequency_stop(108e6)
self.signalshark.scpi.spectrum.set_frequency_start(87.5e6)
# Check RBW:
self.signalshark.scpi.spectrum.set_rbw_auto(True)
rbw = self.signalshark.scpi.spectrum.get_rbw()
# Make measurement as fast as possible:
self.signalshark.scpi.spectrum.set_measurement_time(0.0)
# disconnect from device
self.signalshark.disconnect()
|
Dialog Code Snippets¶
Narda Script Launcher uses the framework wxPython for graphical display. The framework offers many standard dialogs for interaction with the user. Some of these dialogs are described below.
Message Box¶

This is the base method to show a message to the user and to ask for simple decisions.
See also: https://wxpython.org/Phoenix/docs/html/wx.MessageDialog.html
Note
Wrapper function self.MessageBoxModal!
Inside the script method ‘def _run_script(…’, Narda Script Launcher provides a wrapper function ‘self.MessageBoxModal’ which ensures that the MessageBox always remains in the foreground. Please use the wrapper function ‘self.MessageBoxModal’ instead of ‘wx.MessageBox’ here!
You can adapt the behavior and appearance by setting some flags in the method:
flag |
meaning |
---|---|
wx.OK |
show OK button |
wx.CANCEL |
show Cancel button |
wx.YES_NO |
show Yes, No buttons |
wx.YES_DEFAULT |
make Yes button the default |
wx.NO_DEFAULT |
make No button the default |
wx.ICON_EXCLAMATION |
show an alert icon |
wx.ICON_ERROR |
show an error icon |
wx.ICON_HAND |
same as wx.ICON_ERROR |
wx.ICON_INFORMATION |
show an info icon |
wx.ICON_QUESTION |
show a question icon |
Inside script method ‘def _run_script(…’¶
1 2 3 4 5 6 7 8 9 10 | # Inform the user about something
self.MessageBoxModal('Text', 'Caption', wx.OK | wx.ICON_INFORMATION)
# Ask the user for a decision
if self.MessageBoxModal('Text', 'Caption', wx.YES_NO | wx.ICON_QUESTION) == wx.YES:
# Do something
print('User chooses YES')
else:
# Do something
print('User chooses NO')
|
Outside script method ‘def _run_script(…’¶
Note
The parameter ‘parent’ must be replaced by the parent window of the MessageDialog (e.g. a dialog).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | # Inform the user about something
with wx.MessageDialog(parent, 'Text', 'Caption',
wx.OK | wx.STAY_ON_TOP | wx.ICON_INFORMATION) as dlg:
dlg.ShowModal()
# Ask the user for a decision
with wx.MessageDialog(parent, 'Text', 'Caption',
wx.YES_NO | wx.STAY_ON_TOP | wx.ICON_INFORMATION) as dlg:
if dlg.ShowModal() == wx.ID_YES:
# Do something
print('User chooses YES')
else:
# Do something
print('User chooses NO')
|
Wrapper Function ‘self.ShowDlgModalTop(dlg)’!!!¶
Inside the script method ‘def _run_script(…’, Narda Script Launcher provides a wrapper function ‘self.ShowDlgModalTop(dlg)’ which ensures that the dialog always remains in the foreground.
Please use ‘self.frm_dlghelper’ as dialog parent and the wrapper function ‘self.ShowDlgModalTop(dlg)’ instead of ‘dlg.ShowModal()’ here!
TextEntryDialog¶

This dialog can be used to ask the user to enter some text.
See also: https://wxpython.org/Phoenix/docs/html/wx.TextEntryDialog.html
Inside script method ‘def _run_script(…’¶
1 2 3 4 5 6 | # Ask the user to enter some text
dlg = wx.TextEntryDialog(self.frm_dlghelper, 'Enter some text', 'Caption')
dlg.SetValue('Default text')
if self.ShowDlgModalTop(dlg) == wx.ID_OK:
print('You entered:' + dlg.GetValue())
dlg.Destroy() # Please do not forget to destroy the created dialog if you do not need it anymore
|
Outside script method ‘def _run_script(…’¶
Note
The parameter ‘parent’ must be replaced by a modal parent window (e.g. a dialog).
1 2 3 4 5 6 | # Ask the user to enter some text
with wx.TextEntryDialog(parent, message='Enter some text', caption='wx.TextEntryDialog',
value='Default text',
style = wx.TextEntryDialogStyle | wx.STAY_ON_TOP) as dlg:
if dlg.ShowModal() == wx.ID_OK:
print('You entered:' + dlg.GetValue())
|
MultiChoiceDialog¶

This dialog can be used to let the user choose some items from a list.
See also: https://wxpython.org/Phoenix/docs/html/wx.MultiChoiceDialog.html
Inside script method ‘def _run_script(…’¶
1 2 3 4 5 6 7 8 9 10 11 12 | # Ask the user to select a language
lst = ['English', 'German', 'Spanish', 'French'] # Item list
def_sel = [1, 2] # Default selection
dlg = wx.MultiChoiceDialog(self.frm_dlghelper,
'Pick your language(s)',
'wx.MultiChoiceDialog', lst)
dlg.SetSelections(def_sel)
if self.ShowDlgModalTop(dlg) == wx.ID_OK:
selections = dlg.GetSelections()
strings = [lst[x] for x in selections]
print('You chose:' + str(strings))
dlg.Destroy() # Please do not forget to destroy the created dialog if you do not need it anymore
|
Outside script method ‘def _run_script(…’¶
Note
The parameter ‘parent’ must be replaced by a modal parent window (e.g. a dialog).
1 2 3 4 5 6 7 8 9 10 | # Ask the user to select a language
lst = ['English', 'German', 'Spanish', 'French'] # Item list
def_sel = [1, 2] # Default selection
with wx.MultiChoiceDialog(parent, 'Pick your language(s)', 'wx.MultiChoiceDialog',
lst, style=wx.CHOICEDLG_STYLE | wx.STAY_ON_TOP) as dlg:
dlg.SetSelections(def_sel)
if dlg.ShowModal() == wx.ID_OK:
selections = dlg.GetSelections()
strings = [lst[x] for x in selections]
print('You chose:' + str(strings))
|
FileDialog¶
This dialog can be used to ask the user for a file name.
See also: https://wxpython.org/Phoenix/docs/html/wx.FileDialog.html
Open File Dialog

Inside script method ‘def _run_script(…’¶
1 2 3 4 5 6 7 8 9 | # Open File Dialog
dlg = wx.FileDialog(self.frm_dlghelper, 'Open', '', '',
'Text files (*.txt)|*.txt',
wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
if self.ShowDlgModalTop(dlg) == wx.ID_OK:
# Do someting with the filepath
print(dlg.GetPath())
dlg.Destroy() # Please do not forget to destroy the created dialog if you do not need it anymore
|
Outside script method ‘def _run_script(…’¶
Note
The parameter ‘parent’ must be replaced by a modal parent window (e.g. a dialog).
1 2 3 4 5 6 | # Open File Dialog
with wx.FileDialog(parent, 'Open', '', '', 'Text files (*.txt)|*.txt',
style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST | wx.STAY_ON_TOP) as dlg:
if dlg.ShowModal() == wx.ID_OK:
# Do someting with the filepath
print(dlg.GetPath())
|
Save File Dialog

For this example you have to add the following import statement to the import section of your module.
1 | import datetime as dt
|
Inside script method ‘def _run_script(…’¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | # Sets a default file name consisting of a string
# and the current date and time.
initdir = '/'
default_file_name = dt.datetime.now().strftime('measurement_%Y-%m-%d_%H-%M.csv')
wildcard = 'CSV file (*.csv)|*.csv'
filename = ''
dlg = wx.FileDialog(
self.frm_dlghelper, message='Save measurement data to',
defaultDir=initdir,
defaultFile=default_file_name,
wildcard=wildcard,
style=wx.FD_SAVE
)
if self.ShowDlgModalTop(dlg) == wx.ID_OK:
filename = dlg.GetPath()
print(filename)
dlg.Destroy() # Please do not forget to destroy the created dialog if you do not need it anymore
|
Outside script method ‘def _run_script(…’¶
Note
The parameter ‘parent’ must be replaced by a modal parent window (e.g. a dialog).
1 2 3 4 5 6 7 8 9 10 11 12 13 | # Generate a default file name consisting of a string and the current date and time
default_file_name = dt.datetime.now().strftime('measurement_%Y-%m-%d_%H-%M.csv')
filename = ''
initdir = '/'
wildcard = 'CSV file (*.csv)|*.csv'
with wx.FileDialog(parent, message='Save measurement data to',
defaultDir=initdir,
defaultFile=default_file_name,
wildcard=wildcard,
style=wx.FD_SAVE | wx.STAY_ON_TOP) as dlg:
if dlg.ShowModal() == wx.ID_OK:
filename = dlg.GetPath()
print(filename)
|
DirDialog¶

This dialog can be used to ask the user to choose a folder.
See also: https://wxpython.org/Phoenix/docs/html/wx.DirDialog.html
Inside script method ‘def _run_script(…’¶
1 2 3 4 5 | dlg = wx.DirDialog(self.frm_dlghelper, 'Choose a folder:',
style=wx.DD_DEFAULT_STYLE | wx.DD_DIR_MUST_EXIST | wx.DD_CHANGE_DIR)
if self.ShowDlgModalTop(dlg) == wx.ID_OK:
print('Folder: %s' % dlg.GetPath())
dlg.Destroy() # Please do not forget to destroy the created dialog if you do not need it anymore
|
Outside script method ‘def _run_script(…’¶
Note
The parameter ‘parent’ must be replaced by a modal parent window (e.g. a dialog).
1 2 3 4 | with wx.DirDialog(self.frm_dlghelper, 'Choose a folder:',
style=wx.DD_DEFAULT_STYLE | wx.DD_DIR_MUST_EXIST | wx.DD_CHANGE_DIR) as dlg:
if dlg.ShowModal() == wx.ID_OK:
print('Folder: %s' % dlg.GetPath())
|
ListMsgBox (nardascripting.base.toolbox)¶

This dialog is part of the nardascripting toolbox an can be used to let the user select a value from a combobox.
For this example you have to add the following import statement to the import section of your module:
1 | import nardascripting.base.toolbox as tb
|
Inside script method ‘def _run_script(…’¶
1 2 3 4 5 6 7 8 | value_list = ['1', '2', '3', '4', '5'] # value list as string
default_value = '1'
dlg = tb.ListMsgBox(self.frm_dlghelper, 'Designator: ',
value_list, ' Unit', default_value, 'Title')
if self.ShowDlgModalTop(dlg) == wx.ID_OK:
selected_value = float(dlg.value)
print(selected_value)
dlg.Destroy() # Please do not forget to destroy the created dialog if you do not need it anymore
|
Outside script method ‘def _run_script(…’¶
Note
The parameter ‘parent’ must be replaced by a modal parent window (e.g. a dialog).
1 2 3 4 5 6 7 8 | value_list = ['1', '2', '3', '4', '5'] # value list as string
default_value = '1'
with tb.ListMsgBox(parent, 'Designator: ',
value_list, ' Unit', default_value, 'Title') as dlg:
dlg.SetWindowStyle(dlg.GetWindowStyle() | wx.STAY_ON_TOP)
if dlg.ShowModal() == wx.ID_OK:
selected_value = float(dlg.value)
print(selected_value)
|
More Dialogs¶
There are many more useful wxPython dialogs. The following external links show some examples:
https://pythonspot.com/wxpython-dialogs/
http://www.blog.pythonlibrary.org/2010/06/26/the-dialogs-of-wxpython-part-1-of-2/
http://www.blog.pythonlibrary.org/2010/07/10/the-dialogs-of-wxpython-part-2-of-2/