将元组存储为变量(QComboBox)

Storing Tuple as a Variable (QComboBox)

我的项目有多个组合框下拉菜单,用户对第一个下拉菜单的选择决定了下一个下拉菜单的输出。

示例:

first_dropdown = 'Colored Pencil'
colors = "Black", "Blue", "Brown", "Green", "Grey", "Yellow", "White"

self.example = QComboBox()
self.example.addItem(first_dropdown, [colors])

当我这样做时,出现以下错误:

TypeError: index 0 has type 'tuple' but 'str' is expected

The program executes without errors if instead of using the variable 'colors', I simply list the entire tuple ("Black", "Blue", etc.)

如何将元组存储为变量并仍然在我的程序中使用它?这可能吗?

谢谢!

当你这样做时:

[colors]

你会得到这个:

[('Black', 'Blue', 'Brown', 'Green', 'Grey', 'Yellow', 'White')]

该函数需要一个由字符串组成的元组,而不是一个字符串元组列表。 您可以通过以下方式进行简单修复:

tuple(colors)