Passing parameters to a dynamically created Button from an Entry widget in Tkinter Passing parameters to a dynamically created Button from an Entry widget in Tkinter tkinter tkinter

Passing parameters to a dynamically created Button from an Entry widget in Tkinter


You could keep a 2D list of all the entries you create. Then when you want to access them, you only need to plug in the row number. Sample implementation:

from Tkinter import *import xml.etree.ElementTree as ETrootframe = Tk()def runScript(text = 'default text', row=None):    entries = entries_by_row[row] if row is not None else []    print 'runScript {}. Contents of entries: {}'.format(text, [entry.get() for entry in entries])xmlString = '''<window>      <Tab id="tabpage5" name="Debugging" type="custom">        <command dest="1" mode="CONFIG" unit="4" id="SET_TIMEOUT" type="COMMAND">          <initiator name="Set Timeout" type="button" />          <parameter editable="true" param_name="PARAM1">31536000</parameter>        </command>        <command dest="1" mode="CONFIG" unit="4" id="SET_thing" type="COMMAND">          <initiator name="Set Timeout" type="button" />          <parameter param_name="PARAM1" >31536000</parameter>          <parameter param_name="PARAM2">5</parameter>        </command>        <command />        </Tab>        </window>'''xmlRoot  = ET.fromstring(xmlString)entries_by_row = []for tab in xmlRoot.iter('Tab'):    row = 0    column = 0    for command in tab.iter('command'):        entries_by_row.append([])        for tag in command.iter() :            #not sure why command tag is here but skipping it            if tag.tag == 'command':                pass                continue            if tag.tag == 'initiator' and tag.attrib['type'] == 'button':                    button = Button(rootframe, text=tag.attrib['name'], command=lambda row=row: runScript('nondefault text', row))                    button.grid(row=row, column=column, sticky='w')                    column +=1            elif tag.tag == 'parameter':                    entry = Entry(rootframe)                    entry.insert(0,tag.text)                    entry.grid(row=row, column=column)                    column +=1                    entries_by_row[-1].append(entry)        row +=1        column = 0rootframe.mainloop()

Result after clicking each button:

runScript nondefault text. Contents of entries: ['31536000']runScript nondefault text. Contents of entries: ['31536000', '5']