Python Forum
[Tkinter] Loop Counter for OptionMenu Creation
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Loop Counter for OptionMenu Creation
#4
Why would any of those work?  None were defined as variables, and python, being a sane language developed for people who are not insane, does not define variables for you unless you do it yourself :p

You'd access them just as you would the elements of any list: through an index.
>>> import tkinter as tk
>>> root = tk.Tk()
>>> categories = []
>>> for i in range(5):
...   cat = tk.StringVar()
...   cat.set("test: {0}".format(i))
...   entry = tk.Entry(root, textvariable=cat)
...   entry.pack()
...   categories.append(cat)
...
>>> categories
[<tkinter.StringVar object at 0x001B7DB0>, <tkinter.StringVar object at 0x01632AD0>, <tkinter.StringVar object at 0x001EE710>, <tkinter.StringVar object at 0x001EE790>, <tkinter.StringVar object at 0x001EE810>]

# at this point, the window has 5 entry fields.  I changed the contents of the second to say "spam"
# here's how we'd retrieve it's contents later...

>>> categories[1].get()
'spam'
Reply


Messages In This Thread
RE: [Tkinter] Loop Counter for OptionMenu Creation - by nilamo - Sep-29-2017, 05:48 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Setting For Loop Counter rturus 2 813 Dec-07-2022, 01:34 PM
Last Post: deanhystad
  tkinter control break a while loop samtal 0 2,421 Apr-29-2021, 08:26 AM
Last Post: samtal
Question [Help] How to end While Loop using counter? {Screenshot attached} vanicci 2 3,106 Aug-02-2018, 10:09 PM
Last Post: vanicci

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020