# def each_key(layout):
# from itertools import accumulate
# from operator import itemgetter
#
# for row,xs in enumerate(layout):
# column_indices = [0] + list(accumulate(map(itemgetter(1),xs)))
# for column,(key,keylen) in zip(column_indices,xs):
# yield row,column,key,keylen
def each_key(layout):
for row,xs in enumerate(layout):
column = 0
for key,keylen in xs:
yield row,column,key,keylen
column += keylen
def main():
...
for row,column,key,keylenin in each_key(layout):
button = Button(root, text=key,command=press).grid(sticky="news", columnspan=keylen, row=row, column=column)
IyBkZWYgZWFjaF9rZXkobGF5b3V0KToKIyAgICBmcm9tIGl0ZXJ0b29scyBpbXBvcnQgYWNjdW11bGF0ZQojICAgIGZyb20gb3BlcmF0b3IgaW1wb3J0IGl0ZW1nZXR0ZXIKIwojICAgIGZvciByb3cseHMgaW4gZW51bWVyYXRlKGxheW91dCk6CiMgICAgICAgIGNvbHVtbl9pbmRpY2VzID0gWzBdICsgbGlzdChhY2N1bXVsYXRlKG1hcChpdGVtZ2V0dGVyKDEpLHhzKSkpCiMgICAgICAgIGZvciBjb2x1bW4sKGtleSxrZXlsZW4pIGluIHppcChjb2x1bW5faW5kaWNlcyx4cyk6CiMgICAgICAgICAgICB5aWVsZCByb3csY29sdW1uLGtleSxrZXlsZW4KCgpkZWYgZWFjaF9rZXkobGF5b3V0KToKICAgIGZvciByb3cseHMgaW4gZW51bWVyYXRlKGxheW91dCk6CiAgICAgICAgY29sdW1uID0gMAogICAgICAgIGZvciBrZXksa2V5bGVuIGluIHhzOgogICAgICAgICAgICB5aWVsZCByb3csY29sdW1uLGtleSxrZXlsZW4KICAgICAgICAgICAgY29sdW1uICs9IGtleWxlbgoKZGVmIG1haW4oKToKICAgIC4uLgoKICAgIGZvciByb3csY29sdW1uLGtleSxrZXlsZW5pbiBpbiBlYWNoX2tleShsYXlvdXQpOgogICAgICAgIGJ1dHRvbiA9IEJ1dHRvbihyb290LCB0ZXh0PWtleSxjb21tYW5kPXByZXNzKS5ncmlkKHN0aWNreT0ibmV3cyIsIGNvbHVtbnNwYW49a2V5bGVuLCByb3c9cm93LCBjb2x1bW49Y29sdW1uKQo=