obeliksz obeliksz - 1 year ago 54
Python Question

How to pass a variable to the dialog's choices propery?

I would like to have a list of connected users, have chosen dialog tui for this.
This is my first little python (3.5) script.

import sys
import psutil
import locale
import dialog
import pprint

locale.setlocale(locale.LC_ALL, '')
d = dialog.Dialog(dialog="dialog")
choices = []
i = 0;
users = psutil.users()
for user in users:
item = ('{0}.'.format(i), user.name)
i += 1
choices.append(('X', "Exit"))
#OUTPUT: [('0.', 'root'), ('1.', 'root'), ('X', 'Exit')]
#code, tag = d.menu("List", choices)
code, tag = d.menu("List", choices=[('0.', 'root'), ('1.', 'root'), ('X', 'Exit')])

My question is, why is that dialog works with the choices defined inline but can
not when I just give the already defined list that is identical to the list provided in the inline definition.

dialog.DialogError: dialog-like terminated due to an error: the dialog-like program exited with status 3 (which was passed to it as the DIALOG_ERROR environment variable). Sometimes, the reason is simply that dialog was given a height or width parameter that is too big for the terminal in use. Its output, with leading and trailing whitespace stripped, was:

Error: Expected at least 6 tokens for --menu, have 4.

Answer Source

It is not enough pass it as a variable, like:

code, tag = d.menu("List", choices)

It should be declared explicitly as:

code, tag = d.menu("List", choices=choices)