Python在列表中定义列表

And*_*lis -7 python tuples list

我试图定义一个表格颜色,其中包含各种其他表格,其中包含代表颜色的元组.

title_label = [
text = (236, 218, 51),
background = (125, 142, 246)
],
start_button = [
text = (32, 40, 145),
background = (236, 235, 136),
pressed = (44, 51, 112)
],
quit_button = [
text = (166, 21, 13),
background = (48, 61, 188),
pressed = (31, 40, 129)
]
Run Code Online (Sandbox Code Playgroud)

但是,这会产生无效的语法错误.这是为什么?

Mar*_*ers 8

列表不采用名称 - 值对.你可能想在这里使用词典:

definitions = {
    'title_label': {
        'text': (236, 218, 51),
        'background': (125, 142, 246)
    },
    'start_button': {
        'text': (32, 40, 145),
        'background': (236, 235, 136),
        'pressed': (44, 51, 112)
    },
    'quit_button': {
        'text': (166, 21, 13),
        'background': (48, 61, 188),
        'pressed': (31, 40, 129)
    }
}
Run Code Online (Sandbox Code Playgroud)

我不确定你在哪里找到你的语法,但它不是有效的Python.使用[...]方括号的Python列表只能采用一系列单独的Python表达式:

some_list = ['one object', {'dictionary': 'value'}, ['another', 'list']]
Run Code Online (Sandbox Code Playgroud)

请参阅Python教程的数据结构部分.