Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
标签变量的Python循环_Python_Python 3.x_Tkinter - Fatal编程技术网

标签变量的Python循环

标签变量的Python循环,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,我正在尝试创建一个循环,该循环将显示每个产品的各个标签标题(Tkinter模块) 使用当前循环,我可以让它在列表中打印我的10个“静态网页标题”,但我还想将变量标签每次增加+1 例如,它应该这样做: Label_1['text'] = static_webpage_1_titles[0] Label_2['text'] = static_webpage_1_titles[1] Label_3['text'] = static_webpage_1_titles[2] for i in range

我正在尝试创建一个循环,该循环将显示每个产品的各个标签标题(Tkinter模块)

使用当前循环,我可以让它在列表中打印我的10个“静态网页标题”,但我还想将变量标签每次增加+1

例如,它应该这样做:

Label_1['text'] = static_webpage_1_titles[0]
Label_2['text'] = static_webpage_1_titles[1]
Label_3['text'] = static_webpage_1_titles[2]
for i in range(len(static_webpage_1_titles)):
    product_labels[i]['text'] = static_webpage_1_titles[i] +': $'+ static_webpage_1_price[i]
这是我目前的代码:

def Generate_Product_Name_and_Price_1():
    if Button_on:
        Find_static_webpage_1()
        for i in range(len(static_webpage_1_titles)):
            Label_1['text'] = static_webpage_1_titles[i]
编辑:

我在上面为每个标签小部件创建了一个列表,并将循环中的最后一行代码更改为:

def Generate_Product_Name_and_Price_1():
    if Button_on:
        Find_static_webpage_1()
        for i in range(len(static_webpage_1_titles)):
            product_labels[i] = static_webpage_1_titles[i] +': $'+ static_webpage_1_price[i]

运行此操作时,不会收到任何空闲错误,但我的标签小部件不会填充数据。

您需要创建标签实例的列表,而不是标签实例的文本属性。像这样:

product_labels = [Label_1, Label_2, Label_3,
                  Label_4, Label_5, Label_6,
                  Label_7, Label_8, Label_9,
                  Label_10]
然后按如下方式访问属性:

Label_1['text'] = static_webpage_1_titles[0]
Label_2['text'] = static_webpage_1_titles[1]
Label_3['text'] = static_webpage_1_titles[2]
for i in range(len(static_webpage_1_titles)):
    product_labels[i]['text'] = static_webpage_1_titles[i] +': $'+ static_webpage_1_price[i]
您还可以使用zip()使其更整洁:


这是可能的,但这是一个非常糟糕的主意。最好将标签放在一个列表中,然后为列表编制索引。@Novel:这不是不可能的,但你是对的,除非你完全理解为什么不应该这样做,否则这绝对是你不应该做的事情。