Question: Python, Tkinter, Dictionaries and Lists I am trying to work out how to get Python to read items from lists or dictionaries so it can

Python, Tkinter, Dictionaries and Lists

I am trying to work out how to get Python to read items from lists or dictionaries so it can run it as if I typed it into the script file i.e. if I typed "print 'hello world'" and press run Python would print "hello world". If though I had a list, and "print 'hello world'" was an item in that list, how would I get Python to read it from that list and execute it like I had typed it into the script?

If I can work this out, maybe it will help me work my actual problem out:

I have a list called 'watch_option' and the length of that list changes depending on user input. I would like to create spin boxes for each item in that list and place them into my Tkinter window i.e. if there are 4 items, then I would have 4 Spinboxes at different locations in my window. I also need to separate the Spinboxes by a certain number of rows (say 4 for example).

So far I have created a list of unique spinbox names and values, along with a list of unique spinbox grid locations, and then created a dictionary from those lists. I was hoping I could simply call all of the values of the dictionary then and they would be read by the file and create the spinboxes in Tkinter. When I print them in the shell window they appear as though they should work, but I am not sure that it is being read the same way from the dictionary because I don't have any spinboxes showing in my Tkinter when I run it:

spinbox_list = [] spinbox_grid_list = [] for watch, number in enumerate(watch_option): spinbox = 'spinbox_' + str(watch) + ' = Spinbox(window, width = 5, from_=0, to=10)' spinbox_grid = 'spinbox_' + str(watch) + '.grid(padx = 2, pady = 2, row = ' + str(1+ 4*watch) +', column = 0)' spinbox_list.append(spinbox) spinbox_grid_list.append(spinbox_grid)

spinbox_option = {'spinbox_{}'.format(i):e for i, e in enumerate(spinbox_list)} ' '.join(spinbox_option.values()) print ' '.join(spinbox_option.values()) spinbox_grid_option = {'spinbox_grid_{}'.format(i):e for i, e in enumerate(spinbox_grid_list)} ' '.join(spinbox_grid_option.values()) print ' '.join(spinbox_grid_option.values())

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!