[Tkinter] Loop Counter for OptionMenu Creation - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: [Tkinter] Loop Counter for OptionMenu Creation (/thread-5344.html) |
[Tkinter] Loop Counter for OptionMenu Creation - JP_ROMANO - Sep-29-2017 Hello, I have a Tkinter script which creates 15 instances of some entry boxes and two optionmenu widgets (all with the same lists). I know it's not the right way to to it, and am pretty sure sure there is a better way to set everything up than repeating all the code 15 times (which is what I did just to get the app completed on schedule). I would think something like below might work: import tkinter as tk optionsx = ["Select a Category","Item 1", "Item 2", "Item 3"] for n in range(1,16): Category(n) = tk.StringVar(root) Category(n).set(optionsx[0])Which the optimist in me expected to produce: Category1 = tk.StringVar(root) Category2 = tk.StringVar(root) Category3 = tk.StringVar(root) ... Category1.set(optionsx[0]) Category2.set(optionsx[0]) Category3.set(optionsx[0]) ...But I'm getting the error Quote:SyntaxError: can't assign to function call Am I wrong in assuming that there is way to do this without repeating the same things 15 times? Thank you for any input! RE: [Tkinter] Loop Counter for OptionMenu Creation - nilamo - Sep-29-2017 Use a list to keep track of the StringVars, and a loop to create/update/check them. Something like: categories = [] for cat in range(5): var = tk.StringVar(root) var.set(optionsx[0]) categories.append(var) RE: [Tkinter] Loop Counter for OptionMenu Creation - JP_ROMANO - Sep-29-2017 nilamo - thank you! How would I reference those controls then? I've tried using cat1, cat2, cat3, categories1, categories2, categories3 and am getting errors with each try. Quote:NameError: name 'categories1' is not defined Sorry RE: [Tkinter] Loop Counter for OptionMenu Creation - nilamo - Sep-29-2017 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' RE: [Tkinter] Loop Counter for OptionMenu Creation - JP_ROMANO - Sep-29-2017 Well, I quite disagree about the sanity of Python (the Devil's Language), but am certainly grateful for your help and mean no disrespect to your expertise. I thought one of the things that people say make python 'better' is that you don't need to declare variables - it just 'knows'? Anyway, most of the stuff you added above is out of my league, so I'll have to do a bit more reading/research to see if I can reproduce it in my application... or maybe I'm just too old to learn this stuff. Again, I thank you for you time and the guidance! |