我花了两个小时试图缩短这段难看的代码,以便在“customscript#”是我的条目之后立即获取它们的值,我想通过“customscript#.get()”获取值,“rootfr”是我的主框架,s是变量。所以我想知道是否有一种方法,使它与'为'循环或类似的东西,谢谢。你知道吗
customscript1 = Entry(rootfr)
customscript1.insert(0, s1)
customscript1.grid(column = 3, row = 1)
customscript2 = Entry(rootfr)
customscript2.insert(0, s1)
customscript2.grid(column = 3, row = 2)
customscript3 = Entry(rootfr)
customscript3.insert(0, s1)
customscript3.grid(column = 3, row = 3)
customscript4 = Entry(rootfr)
customscript4.insert(0, s1)
customscript4.grid(column = 3, row = 4)
customscript5 = Entry(rootfr)
customscript5.insert(0, s1)
customscript5.grid(column = 3, row = 5)
customscript6 = Entry(rootfr)
customscript6.insert(0, s1)
customscript6.grid(column = 3, row = 6)
我想你可以用
locals()
或globals()
。你知道吗功能参考:
https://docs.python.org/2/library/functions.html#globals
https://docs.python.org/2/library/functions.html#locals
或者您可以简单地使用一个列表来存储所有这些自定义脚本,因为我严重怀疑您是否真的需要一组编号的变量。根据经验,如果您发现自己被迫编写难看的代码,那么问题就出在代码体系结构的某个地方。你知道吗
您可以将条目存储在列表或词典中。我发现字典很方便,因为它们可以是稀疏的(即:你不必从零开始计数):
稍后您可以通过它们的索引访问它们:
相关问题 更多 >
编程相关推荐