Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Automatically naming objects in Tkinter

Status
Not open for further replies.

Athos

Programmer
Jul 31, 2000
1
US
I am creating a window with a variable number of entries powered by a list and a for statement in Tkinter.&nbsp;&nbsp;It is basically like this:<br><br>cnt = 0<br>for x in list:<br> entry = Entry(window)<br> label = Label(window, text=x)<br> label.grid(row=cnt)<br> entry.grid(row=cnt, column=1)<br> cnt = cnt+1<br><br>My problem is that when I want to do a .get() command on the entries they all have the same name, so I'm only able to get the last one.&nbsp;&nbsp;Is there a way for me automatically name the entries so each one has a unique name that I can find and use?<br>
 
Here is one solution:<br><br>cnt = 0<br>win=[]<br>for x in list:<br>&nbsp;&nbsp;entry = Entry(window)<br>&nbsp;&nbsp;label = Label(window, text=x)<br>&nbsp;&nbsp;label.grid(row=cnt)<br>&nbsp;&nbsp;entry.grid(row=cnt, column=1)<br>&nbsp;&nbsp;cnt = cnt+1<br>&nbsp;&nbsp;win.append({'entry': entry,'label': label})<br><br>- now you can access each row as:<br><br>&nbsp;&nbsp;win[0][&quot;entry&quot;] &lt;-- the entry for row:0<br>&nbsp;&nbsp;(to use get: do win[0][&quot;entry&quot;].get() )<br><br><br>a lot of other solutions exist of course<br>some more elegant - some less ...
 
a bit trickier:<br><br>class dummy: pass<br><br>cnt = 0<br>win=[]<br>for x in list:<br>&nbsp;&nbsp;entry = Entry(window)<br>&nbsp;&nbsp;label = Label(window, text=x)<br>&nbsp;&nbsp;label.grid(row=cnt)<br>&nbsp;&nbsp;entry.grid(row=cnt, column=1)<br>&nbsp;&nbsp;cnt = cnt+1<br>&nbsp;&nbsp;row=dummy()<br>&nbsp;&nbsp;row.entry=entry<br>&nbsp;&nbsp;row.label=label<br>&nbsp;&nbsp;win.append(row)<br><br><br>now access is:<br>&nbsp;&nbsp;win[0].entry - for row 0<br>&nbsp;&nbsp;win[cnt-1].entry - for last row..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top