I passed two hours trying to make this ugly code shorter, in order to get their value right after, 'customscript#' are my entries i want to get the value back by 'customscript#.get()', 'rootfr' is my main frame, s# are variables. So I wanted to know if there was a way to make it with a 'for' loop or something like that, thanks.
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)
You can use locals()
or globals()
I guess.
local_dict = locals()
for index in xrange(1, 7):
local_dict['customscript%d' % index] = entry = Entry(rootfr)
entry.insert(0, s1)
entry.grid(column = 3, row = index)
Functions reference:
https://docs.python.org/2/library/functions.html#globals
https://docs.python.org/2/library/functions.html#locals
Or you could simply use a list to store all those customscripts, as I seriously doubt you really need to have a bunch of numbered variables. As a rule of thumb, if you find yourself forced to write ugly code, the problem is somewhere in the code architecture.