【问题标题】:How to pass a variable to the dialog's choices propery?如何将变量传递给对话框选择属性?
【发布时间】:2026-01-16 23:10:02
【问题描述】:

我想要一个已连接用户的列表,为此选择了对话 tui。 这是我的第一个小 python (3.5) 脚本。

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)
        choices.append(item)
        i += 1
choices.append(('X', "Exit"))
#pprint.pprint(choices)
#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')])

我的问题是,为什么该对话框适用于内联定义的选项,但可以 不是当我只是给出与内联定义中提供的列表相同的已定义列表时。

child_output.strip()))
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.

【问题讨论】:

    标签: python dialog


    【解决方案1】:

    将其作为变量传递是不够的,例如:

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

    应该明确声明为:

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

    【讨论】:

      最近更新 更多