Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.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 在for循环中自动创建唯一变量?_Python_Variables_Sqlite_Tkinter - Fatal编程技术网

Python 在for循环中自动创建唯一变量?

Python 在for循环中自动创建唯一变量?,python,variables,sqlite,tkinter,Python,Variables,Sqlite,Tkinter,我在这里写这篇文章是想问,是否有一种方法可以让Python在如下代码的上下文中自动在for循环中创建新的唯一变量: for row in customerdetails2: AA10 = Entry(CustomerEdit) AA10.insert(0, row[0]) AA10.grid(row = editnum, column = 0) AA11 = Entry(CustomerEdit) AA11.insert(0, row[1]) AA

我在这里写这篇文章是想问,是否有一种方法可以让Python在如下代码的上下文中自动在for循环中创建新的唯一变量:

for row in customerdetails2:
    AA10 = Entry(CustomerEdit)
    AA10.insert(0, row[0])
    AA10.grid(row = editnum, column = 0)
    AA11 = Entry(CustomerEdit)
    AA11.insert(0, row[1])
    AA11.grid(row = editnum, column = 1)
    AA12 = Entry(CustomerEdit)
    AA12.insert(0, row[2])
    AA12.grid(row = editnum, column = 2)
    AA13 = Entry(CustomerEdit)
    AA13.insert(0, row[3])
    AA13.grid(row = editnum, column = 3)
    AA14 = Entry(CustomerEdit)
    AA14.insert(0, row[4])
    AA14.grid(row = editnum, column = 4)
因此,基本上对于Python在customerdetails2(它从中收集信息的数据库)中找到的每一行,我希望它创建一组新的输入字段,但变量的名称不同,而不是所有的输入字段都被称为“AA10”、“AA11”、“AA12”等。这可能吗?如果没有,是否有一个可行的替代方案可以达到相同的结果? 抱歉,我对Python还是很陌生,但请提前感谢。

您可以有一系列条目:

entries = []
for index, row in enumerate(customerdetails2):
    entry = Entry(CustomerEdit)
    entry.insert(0, row[index])
    entry.grid(row = editnum, column = index)
    entries.append(entry)
当您需要引用它们时,只需按列表中的索引引用即可

如果需要保留
AA1
前缀,请尝试:


是一个函数,它接受一个iterable对象并遍历该对象,并在该对象中生成索引和该索引处的对象。

为什么不使用列表,甚至循环行[0…4]为什么不在此处使用
列表
或任何其他集合?您好,感谢您的响应。使用列表的解决方案似乎给了我一个问题,程序只显示第1列/第1行、第2列/第2行、第3列/第3行的输入字段,依此类推。你知道是什么引起的吗?此外,我不确定这段代码中“索引”的功能是什么。抱歉,Python新手。添加了
enumerate
的信息。我忘了提到“customerdetails2”是一个由程序中的sqlite3访问的.db文件。这在任何方面都有意义吗?只要您不尝试遍历文件对象,而是循环遍历有效SQL查询的结果,就没有问题。
entries = {}
for index, row in enumerate(customerdetails2):
    entry = Entry(CustomerEdit)
    entry.insert(0, row[index])
    entry.grid(row = editnum, column = index)
    entries['AA1{}'.format(index)] = entry