python - naming objects from a list of names -
i using graphics.py create simple ui. want set number of entry objects fields. there way loop through list of field names , use them variable names create entry objects? or dreaded "dynamic variable" boogeyman seems avoided @ costs?
conceptually, i'd able write this:
from graphics import * window = graphwin("contact data", 200, 200) x = 100 y = 10 field_names = ['name', 'address', 'phone'] field_text = ['al', '123 4 street', '555-666-7777'] in range(len(field_names)): field_names[i] = entry(point(x, y + 30 * i), 15) field_names[i].draw(window) field_names[i].settext(field_text[i]) # don't know how embed image here show window window.getmouse() window.close()
to yield:
print(name) al print(address) 123 4 street print(phone) 555-666-7777
but get:
print(name) traceback (most recent call last): file "d:\googledrive\cmpt103\graphics\untitled-1.py", line 15, in 0 builtins.nameerror: name 'name' not defined
when this:
field_names[i].settext(field_text[i])
what trying do? strings don't have settext
method.
even if did, however, wouldn't setting variables think are. changing field_names
array, first element no longer name
, instead value of name
item.
what want dictionary. first, declare dictionary @ top of script:
values_dict = {}
and set entries in this:
values_dict[field_names[i]] = field_text[i]
after can access them this:
print(values_dict['name'])