×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Automatically naming objects in Tkinter

Automatically naming objects in Tkinter

Automatically naming objects in Tkinter

(OP)
I am creating a window with a variable number of entries powered by a list and a for statement in Tkinter.  It is basically like this:

cnt = 0
for x in list:
entry = Entry(window)
label = Label(window, text=x)
label.grid(row=cnt)
entry.grid(row=cnt, column=1)
cnt = cnt+1

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.  Is there a way for me automatically name the entries so each one has a unique name that I can find and use?

RE: Automatically naming objects in Tkinter

Here is one solution:

cnt = 0
win=[]
for x in list:
  entry = Entry(window)
  label = Label(window, text=x)
  label.grid(row=cnt)
  entry.grid(row=cnt, column=1)
  cnt = cnt+1
  win.append({'entry': entry,'label': label})

- now you can access each row as:

  win[0]["entry"] <-- the entry for row:0
  (to use get: do win[0]["entry"].get() )


a lot of other solutions exist of course
some more elegant - some less ...

RE: Automatically naming objects in Tkinter

a bit trickier:

class dummy: pass

cnt = 0
win=[]
for x in list:
  entry = Entry(window)
  label = Label(window, text=x)
  label.grid(row=cnt)
  entry.grid(row=cnt, column=1)
  cnt = cnt+1
  row=dummy()
  row.entry=entry
  row.label=label
  win.append(row)


now access is:
  win[0].entry - for row 0
  win[cnt-1].entry - for last row..

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close