How to create buttons, text boxes and label in loop in tkinter
<p>I am trying to create a framework to create a label, text box and button as an object, I can extend it easily.</p>

<p>Idea:</p>

<p><a href="http://i.stack.imgur.com/gsSAR.png" rel="nofollow">Original</a></p>

<p><a href="http://i.stack.imgur.com/x8q8C.png" rel="nofollow">after extend</a></p>

<p>and it could extend to have 3, 4, 5 or more if it have more file, just declare in dictionary list, it will extend automatically. </p>

<p>The code I have:</p>

<pre><code>def getpath(entry_box, type):
# path or file
if type == 'path':
    entry_box.set(filedial.askdirectory())
elif type == 'file':
    entry_box.set(filedial.askopenfilename())

MainWin = tk.Tk() # Create main windows
MainWin.title('Get file and path') # App name

# Create root container to hold Frames
mainFrame = ttk.Frame(MainWin)
mainFrame.grid(column=1, row=1)
# define the object to create in dictionary, format:
# {object name:[title name, default path, button name, file or folder path]}
obj2create ={'file1':['ABC Location: ', r'C:/', 'Get ABC','file'],
             'file2': ['DEF Location:', r'C:/1/', 'Get DEF', 'file']}
ttl_obj = 0
for key in obj2create:
    ttl_obj +=1
    vir = obj2create[key]
    # Module for get file:
    obj_name = key
    title = vir[0]
    default_path = vir[1]
    btn_name = vir[2]
    get_type = vir[3]

    # Create main container
    pa_frame = ttk.Frame(mainFrame)
    pa_frame.grid(column=1, row=10*ttl_obj, sticky=tk.W)
    pa_frame.config(width = 100)
    # Row 1: Label
    frame_name = obj_name + '_name'
    print(frame_name)
    frame_name = ttk.Label(pa_frame, text= title).grid(column=1, row=10*ttl_obj,sticky=tk.W)
    # Row 2: path and button
    # assign type
    path_loc = obj_name + '_path'
    path_loc = tk.StringVar()
    path_loc.set(default_path)
    # put in frame
    fileLocPath = obj_name + '_loc_path'
    fileLocPath = ttk.Entry(pa_frame, width=70, textvariable=path_loc)
    fileLocPath.grid(column=1, row=30*ttl_obj) # Assign position
    # Get file button
    # define button display text and assign the command / function
    bt_get_file_path = obj_name + '_btn'
    bt_get_file_path = ttk.Button(pa_frame, text= btn_name,
                                  command=lambda: getpath(path_loc, get_type))
    # Position Button in second row, second column (zero-based)
    bt_get_file_path.grid(column=2, row=30*ttl_obj)


# Auto popup when open
MainWin.mainloop() # Let the window keep running until close
</code></pre>

<p><strong>Issue:</strong></p>

<ol>
<li><p>Default file path appear in second text box only.</p></li>
<li><p>All the button location point to second box.</p></li>
</ol>

<p>I also not sure how could I get the value in different box, the "path_loc = obj_name + '_path'" not able to get the correct object.</p>

<p>How should I make it work? Or the way I use is wrong?</p>

<p>You have a lot of unnecessary code written like creating a string and assigning a widget to the same variable.<br>
Now, I am not certain what the code should look like but I'll give you a way where, in case of making new variables every time in a loop, there is this built-in function <code>exec</code> that might help.<br>
This funtion will help you use string as a python syntax command.<br>
for e.g:</p>

<pre><code>for i in range(5):
  exec ("lbl_%s = tk.Label(mainFrame)" % (str(i)) )
</code></pre>

<p>In this above way, the <code>exec</code> will treat the string as a python command/syntax, so it will actually create 5 Tkinter labels:</p>

<pre><code>In [10]: lbl_0
Out[10]: &lt;Tkinter.Label instance at 0x03471C10&gt;

In [11]: lbl_1
Out[11]: &lt;Tkinter.Label instance at 0x03442828&gt;

...
...

In [14]: lbl_4
Out[14]: &lt;Tkinter.Label instance at 0x03471B20&gt;
</code></pre>

<p>Now this way you would be able to access those objects using their variable names and also would be able to modify them individually.</p>

python tkinter