Tkinter Filedialog.askopenfilename iteration












1















I am developing program to load up files and perform some calculations with those loaded files.



For that I wrote a simple iteration code to load the tkinter variables. The window, label, entry and button positions are already done. So far the code I have is:



import tkinter as tk
from tkinter import ttk, filedialog

LARGE_FONT = ("Arial", 12)
MEDIUM_FONT = ("Arial", 11)
REGULAR_FONT = ("Arial", 10)

text_z = ["Select file 1", "Select the file 2", "Select file 3", "Select file 4"]

window=tk.Tk()

def click():
z = tk.filedialog.askopenfilename(initialdir = "/",title = "Select file", filetypes = ( ("Excel file", "*.xlsx"), ("All files", "*.*") ) )
a[i-2].insert(tk.END, z)
z[i] = a[i-2].get()

##Main program
#There is an image I will add at the end on row=0
ttk.Label(window, text="file load", font = LARGE_FONT, background = "white").grid(row=1, column=1, columnspan=3, padx=20, pady = 10, sticky="W")

a = [tk.StringVar(window) for i in range(len(text_z))]

for i in range(2,len(text_z)+2):
Label_z = ttk.Label(window, text=text_z[i-2], background="white").grid(row= 2*i, column=0,columnspan=3, padx=10, pady=2, sticky="W")
a[i-2] = ttk.Entry(window, width=60, background="gray")
a[i-2].grid(row= 2*i+1, column=0, columnspan=3, padx=10, sticky="WE")
ttk.Button(window, text="Search", width=10, command=click).grid(row= 2*i+1, column=3, padx=5, sticky="W")

window.mainloop()


My problem is on the click button. It was supposed to during a click run the askopenfilename, get the file path and present on the entrybox, but all the buttons direct that to the last created Entrybox.



Can someone help me with this issue?



Thanks alot!










share|improve this question

























  • Sorry @Miraj50, edited the question with those items

    – Rafael Castelo Branco
    Nov 14 '18 at 13:12
















1















I am developing program to load up files and perform some calculations with those loaded files.



For that I wrote a simple iteration code to load the tkinter variables. The window, label, entry and button positions are already done. So far the code I have is:



import tkinter as tk
from tkinter import ttk, filedialog

LARGE_FONT = ("Arial", 12)
MEDIUM_FONT = ("Arial", 11)
REGULAR_FONT = ("Arial", 10)

text_z = ["Select file 1", "Select the file 2", "Select file 3", "Select file 4"]

window=tk.Tk()

def click():
z = tk.filedialog.askopenfilename(initialdir = "/",title = "Select file", filetypes = ( ("Excel file", "*.xlsx"), ("All files", "*.*") ) )
a[i-2].insert(tk.END, z)
z[i] = a[i-2].get()

##Main program
#There is an image I will add at the end on row=0
ttk.Label(window, text="file load", font = LARGE_FONT, background = "white").grid(row=1, column=1, columnspan=3, padx=20, pady = 10, sticky="W")

a = [tk.StringVar(window) for i in range(len(text_z))]

for i in range(2,len(text_z)+2):
Label_z = ttk.Label(window, text=text_z[i-2], background="white").grid(row= 2*i, column=0,columnspan=3, padx=10, pady=2, sticky="W")
a[i-2] = ttk.Entry(window, width=60, background="gray")
a[i-2].grid(row= 2*i+1, column=0, columnspan=3, padx=10, sticky="WE")
ttk.Button(window, text="Search", width=10, command=click).grid(row= 2*i+1, column=3, padx=5, sticky="W")

window.mainloop()


My problem is on the click button. It was supposed to during a click run the askopenfilename, get the file path and present on the entrybox, but all the buttons direct that to the last created Entrybox.



Can someone help me with this issue?



Thanks alot!










share|improve this question

























  • Sorry @Miraj50, edited the question with those items

    – Rafael Castelo Branco
    Nov 14 '18 at 13:12














1












1








1








I am developing program to load up files and perform some calculations with those loaded files.



For that I wrote a simple iteration code to load the tkinter variables. The window, label, entry and button positions are already done. So far the code I have is:



import tkinter as tk
from tkinter import ttk, filedialog

LARGE_FONT = ("Arial", 12)
MEDIUM_FONT = ("Arial", 11)
REGULAR_FONT = ("Arial", 10)

text_z = ["Select file 1", "Select the file 2", "Select file 3", "Select file 4"]

window=tk.Tk()

def click():
z = tk.filedialog.askopenfilename(initialdir = "/",title = "Select file", filetypes = ( ("Excel file", "*.xlsx"), ("All files", "*.*") ) )
a[i-2].insert(tk.END, z)
z[i] = a[i-2].get()

##Main program
#There is an image I will add at the end on row=0
ttk.Label(window, text="file load", font = LARGE_FONT, background = "white").grid(row=1, column=1, columnspan=3, padx=20, pady = 10, sticky="W")

a = [tk.StringVar(window) for i in range(len(text_z))]

for i in range(2,len(text_z)+2):
Label_z = ttk.Label(window, text=text_z[i-2], background="white").grid(row= 2*i, column=0,columnspan=3, padx=10, pady=2, sticky="W")
a[i-2] = ttk.Entry(window, width=60, background="gray")
a[i-2].grid(row= 2*i+1, column=0, columnspan=3, padx=10, sticky="WE")
ttk.Button(window, text="Search", width=10, command=click).grid(row= 2*i+1, column=3, padx=5, sticky="W")

window.mainloop()


My problem is on the click button. It was supposed to during a click run the askopenfilename, get the file path and present on the entrybox, but all the buttons direct that to the last created Entrybox.



Can someone help me with this issue?



Thanks alot!










share|improve this question
















I am developing program to load up files and perform some calculations with those loaded files.



For that I wrote a simple iteration code to load the tkinter variables. The window, label, entry and button positions are already done. So far the code I have is:



import tkinter as tk
from tkinter import ttk, filedialog

LARGE_FONT = ("Arial", 12)
MEDIUM_FONT = ("Arial", 11)
REGULAR_FONT = ("Arial", 10)

text_z = ["Select file 1", "Select the file 2", "Select file 3", "Select file 4"]

window=tk.Tk()

def click():
z = tk.filedialog.askopenfilename(initialdir = "/",title = "Select file", filetypes = ( ("Excel file", "*.xlsx"), ("All files", "*.*") ) )
a[i-2].insert(tk.END, z)
z[i] = a[i-2].get()

##Main program
#There is an image I will add at the end on row=0
ttk.Label(window, text="file load", font = LARGE_FONT, background = "white").grid(row=1, column=1, columnspan=3, padx=20, pady = 10, sticky="W")

a = [tk.StringVar(window) for i in range(len(text_z))]

for i in range(2,len(text_z)+2):
Label_z = ttk.Label(window, text=text_z[i-2], background="white").grid(row= 2*i, column=0,columnspan=3, padx=10, pady=2, sticky="W")
a[i-2] = ttk.Entry(window, width=60, background="gray")
a[i-2].grid(row= 2*i+1, column=0, columnspan=3, padx=10, sticky="WE")
ttk.Button(window, text="Search", width=10, command=click).grid(row= 2*i+1, column=3, padx=5, sticky="W")

window.mainloop()


My problem is on the click button. It was supposed to during a click run the askopenfilename, get the file path and present on the entrybox, but all the buttons direct that to the last created Entrybox.



Can someone help me with this issue?



Thanks alot!







python tkinter iteration openfiledialog






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 14:44









Miraj50

2,7701924




2,7701924










asked Nov 14 '18 at 12:54









Rafael Castelo BrancoRafael Castelo Branco

395




395













  • Sorry @Miraj50, edited the question with those items

    – Rafael Castelo Branco
    Nov 14 '18 at 13:12



















  • Sorry @Miraj50, edited the question with those items

    – Rafael Castelo Branco
    Nov 14 '18 at 13:12

















Sorry @Miraj50, edited the question with those items

– Rafael Castelo Branco
Nov 14 '18 at 13:12





Sorry @Miraj50, edited the question with those items

– Rafael Castelo Branco
Nov 14 '18 at 13:12












2 Answers
2






active

oldest

votes


















1














Lambda to the rescue. One needs to know the right Button-Entry pair to update. So pass the value of the corresponding index when a button is pressed.



import tkinter as tk
from tkinter import ttk, filedialog

LARGE_FONT = ("Arial", 12)
MEDIUM_FONT = ("Arial", 11)
REGULAR_FONT = ("Arial", 10)

text_z = ["Select file 1", "Select the file 2", "Select file 3", "Select file 4"]

window=tk.Tk()

def click(m):
z = tk.filedialog.askopenfilename(initialdir = "~",title = "Select file", filetypes = ( ("Text files", "*.txt"), ("All files", "*.*") ) )
a[m].insert(tk.END, z)

ttk.Label(window, text="file load", font = LARGE_FONT, background = "white").grid(row=1, column=1, columnspan=3, padx=20, pady = 10, sticky="W")

a = [None for i in range(len(text_z))]

for i in range(2,len(text_z)+2):
Label_z = ttk.Label(window, text=text_z[i-2], background="white").grid(row= 2*i, column=0,columnspan=3, padx=10, pady=2, sticky="W")
a[i-2] = ttk.Entry(window, width=60, background="gray")
a[i-2].grid(row= 2*i+1, column=0, columnspan=3, padx=10, sticky="WE")
ttk.Button(window, text="Search", width=10, command=lambda m=i-2:click(m)).grid(row= 2*i+1, column=3, padx=5, sticky="W")

window.mainloop()


enter image description here






share|improve this answer

































    0














    I think you should simplify things a bit by use a list to store your entry fields.
    To do this I think it would be best to add frames for each set of widgets and to use the index of range to get what we need.



    I have changed up your code a little to make it easier to work with list index as well as added a button that will print out each selected path on each entry field to show these values are accessible.



    import tkinter as tk
    from tkinter import ttk, filedialog

    LARGE_FONT = ("Arial", 12)
    MEDIUM_FONT = ("Arial", 11)
    REGULAR_FONT = ("Arial", 10)

    text_z = ["Select file 1", "Select the file 2", "Select file 3", "Select file 4"]

    window = tk.Tk()

    def click(x):
    z = tk.filedialog.askopenfilename(initialdir="/", title="Select file", filetypes=(("Excel file", "*.xlsx"), ("All files", "*.*")))
    a[x].insert(tk.END, z)

    ttk.Label(window, text="file load", font=LARGE_FONT, background="white").grid(row=1, column=0, padx=20, pady=10, sticky="w")

    a=

    for i in range(len(text_z)):
    frame = tk.Frame(window)
    frame.grid(row=i+2, column=0, sticky="nsew")
    ttk.Label(frame, text=text_z[i], background="white").grid(row=0, column=0, columnspan=3, padx=10, pady=2, sticky="w")
    a.append(ttk.Entry(frame, width=60, background="gray"))
    a[i].grid(row=1, column=0, columnspan=3, padx=10, sticky="ew")
    ttk.Button(frame, text="Search", width=10, command=lambda x=i: click(x)).grid(row=1, column=3, padx=5, sticky="w")

    def pring_current_paths():
    for ndex, entry in enumerate(a):
    print("Entry {}: ".format(ndex, entry.get()))

    tk.Button(window, text="Print gurrent paths!", command=pring_current_paths).grid()

    window.mainloop()





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53300739%2ftkinter-filedialog-askopenfilename-iteration%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      Lambda to the rescue. One needs to know the right Button-Entry pair to update. So pass the value of the corresponding index when a button is pressed.



      import tkinter as tk
      from tkinter import ttk, filedialog

      LARGE_FONT = ("Arial", 12)
      MEDIUM_FONT = ("Arial", 11)
      REGULAR_FONT = ("Arial", 10)

      text_z = ["Select file 1", "Select the file 2", "Select file 3", "Select file 4"]

      window=tk.Tk()

      def click(m):
      z = tk.filedialog.askopenfilename(initialdir = "~",title = "Select file", filetypes = ( ("Text files", "*.txt"), ("All files", "*.*") ) )
      a[m].insert(tk.END, z)

      ttk.Label(window, text="file load", font = LARGE_FONT, background = "white").grid(row=1, column=1, columnspan=3, padx=20, pady = 10, sticky="W")

      a = [None for i in range(len(text_z))]

      for i in range(2,len(text_z)+2):
      Label_z = ttk.Label(window, text=text_z[i-2], background="white").grid(row= 2*i, column=0,columnspan=3, padx=10, pady=2, sticky="W")
      a[i-2] = ttk.Entry(window, width=60, background="gray")
      a[i-2].grid(row= 2*i+1, column=0, columnspan=3, padx=10, sticky="WE")
      ttk.Button(window, text="Search", width=10, command=lambda m=i-2:click(m)).grid(row= 2*i+1, column=3, padx=5, sticky="W")

      window.mainloop()


      enter image description here






      share|improve this answer






























        1














        Lambda to the rescue. One needs to know the right Button-Entry pair to update. So pass the value of the corresponding index when a button is pressed.



        import tkinter as tk
        from tkinter import ttk, filedialog

        LARGE_FONT = ("Arial", 12)
        MEDIUM_FONT = ("Arial", 11)
        REGULAR_FONT = ("Arial", 10)

        text_z = ["Select file 1", "Select the file 2", "Select file 3", "Select file 4"]

        window=tk.Tk()

        def click(m):
        z = tk.filedialog.askopenfilename(initialdir = "~",title = "Select file", filetypes = ( ("Text files", "*.txt"), ("All files", "*.*") ) )
        a[m].insert(tk.END, z)

        ttk.Label(window, text="file load", font = LARGE_FONT, background = "white").grid(row=1, column=1, columnspan=3, padx=20, pady = 10, sticky="W")

        a = [None for i in range(len(text_z))]

        for i in range(2,len(text_z)+2):
        Label_z = ttk.Label(window, text=text_z[i-2], background="white").grid(row= 2*i, column=0,columnspan=3, padx=10, pady=2, sticky="W")
        a[i-2] = ttk.Entry(window, width=60, background="gray")
        a[i-2].grid(row= 2*i+1, column=0, columnspan=3, padx=10, sticky="WE")
        ttk.Button(window, text="Search", width=10, command=lambda m=i-2:click(m)).grid(row= 2*i+1, column=3, padx=5, sticky="W")

        window.mainloop()


        enter image description here






        share|improve this answer




























          1












          1








          1







          Lambda to the rescue. One needs to know the right Button-Entry pair to update. So pass the value of the corresponding index when a button is pressed.



          import tkinter as tk
          from tkinter import ttk, filedialog

          LARGE_FONT = ("Arial", 12)
          MEDIUM_FONT = ("Arial", 11)
          REGULAR_FONT = ("Arial", 10)

          text_z = ["Select file 1", "Select the file 2", "Select file 3", "Select file 4"]

          window=tk.Tk()

          def click(m):
          z = tk.filedialog.askopenfilename(initialdir = "~",title = "Select file", filetypes = ( ("Text files", "*.txt"), ("All files", "*.*") ) )
          a[m].insert(tk.END, z)

          ttk.Label(window, text="file load", font = LARGE_FONT, background = "white").grid(row=1, column=1, columnspan=3, padx=20, pady = 10, sticky="W")

          a = [None for i in range(len(text_z))]

          for i in range(2,len(text_z)+2):
          Label_z = ttk.Label(window, text=text_z[i-2], background="white").grid(row= 2*i, column=0,columnspan=3, padx=10, pady=2, sticky="W")
          a[i-2] = ttk.Entry(window, width=60, background="gray")
          a[i-2].grid(row= 2*i+1, column=0, columnspan=3, padx=10, sticky="WE")
          ttk.Button(window, text="Search", width=10, command=lambda m=i-2:click(m)).grid(row= 2*i+1, column=3, padx=5, sticky="W")

          window.mainloop()


          enter image description here






          share|improve this answer















          Lambda to the rescue. One needs to know the right Button-Entry pair to update. So pass the value of the corresponding index when a button is pressed.



          import tkinter as tk
          from tkinter import ttk, filedialog

          LARGE_FONT = ("Arial", 12)
          MEDIUM_FONT = ("Arial", 11)
          REGULAR_FONT = ("Arial", 10)

          text_z = ["Select file 1", "Select the file 2", "Select file 3", "Select file 4"]

          window=tk.Tk()

          def click(m):
          z = tk.filedialog.askopenfilename(initialdir = "~",title = "Select file", filetypes = ( ("Text files", "*.txt"), ("All files", "*.*") ) )
          a[m].insert(tk.END, z)

          ttk.Label(window, text="file load", font = LARGE_FONT, background = "white").grid(row=1, column=1, columnspan=3, padx=20, pady = 10, sticky="W")

          a = [None for i in range(len(text_z))]

          for i in range(2,len(text_z)+2):
          Label_z = ttk.Label(window, text=text_z[i-2], background="white").grid(row= 2*i, column=0,columnspan=3, padx=10, pady=2, sticky="W")
          a[i-2] = ttk.Entry(window, width=60, background="gray")
          a[i-2].grid(row= 2*i+1, column=0, columnspan=3, padx=10, sticky="WE")
          ttk.Button(window, text="Search", width=10, command=lambda m=i-2:click(m)).grid(row= 2*i+1, column=3, padx=5, sticky="W")

          window.mainloop()


          enter image description here







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 '18 at 14:03

























          answered Nov 14 '18 at 13:46









          Miraj50Miraj50

          2,7701924




          2,7701924

























              0














              I think you should simplify things a bit by use a list to store your entry fields.
              To do this I think it would be best to add frames for each set of widgets and to use the index of range to get what we need.



              I have changed up your code a little to make it easier to work with list index as well as added a button that will print out each selected path on each entry field to show these values are accessible.



              import tkinter as tk
              from tkinter import ttk, filedialog

              LARGE_FONT = ("Arial", 12)
              MEDIUM_FONT = ("Arial", 11)
              REGULAR_FONT = ("Arial", 10)

              text_z = ["Select file 1", "Select the file 2", "Select file 3", "Select file 4"]

              window = tk.Tk()

              def click(x):
              z = tk.filedialog.askopenfilename(initialdir="/", title="Select file", filetypes=(("Excel file", "*.xlsx"), ("All files", "*.*")))
              a[x].insert(tk.END, z)

              ttk.Label(window, text="file load", font=LARGE_FONT, background="white").grid(row=1, column=0, padx=20, pady=10, sticky="w")

              a=

              for i in range(len(text_z)):
              frame = tk.Frame(window)
              frame.grid(row=i+2, column=0, sticky="nsew")
              ttk.Label(frame, text=text_z[i], background="white").grid(row=0, column=0, columnspan=3, padx=10, pady=2, sticky="w")
              a.append(ttk.Entry(frame, width=60, background="gray"))
              a[i].grid(row=1, column=0, columnspan=3, padx=10, sticky="ew")
              ttk.Button(frame, text="Search", width=10, command=lambda x=i: click(x)).grid(row=1, column=3, padx=5, sticky="w")

              def pring_current_paths():
              for ndex, entry in enumerate(a):
              print("Entry {}: ".format(ndex, entry.get()))

              tk.Button(window, text="Print gurrent paths!", command=pring_current_paths).grid()

              window.mainloop()





              share|improve this answer




























                0














                I think you should simplify things a bit by use a list to store your entry fields.
                To do this I think it would be best to add frames for each set of widgets and to use the index of range to get what we need.



                I have changed up your code a little to make it easier to work with list index as well as added a button that will print out each selected path on each entry field to show these values are accessible.



                import tkinter as tk
                from tkinter import ttk, filedialog

                LARGE_FONT = ("Arial", 12)
                MEDIUM_FONT = ("Arial", 11)
                REGULAR_FONT = ("Arial", 10)

                text_z = ["Select file 1", "Select the file 2", "Select file 3", "Select file 4"]

                window = tk.Tk()

                def click(x):
                z = tk.filedialog.askopenfilename(initialdir="/", title="Select file", filetypes=(("Excel file", "*.xlsx"), ("All files", "*.*")))
                a[x].insert(tk.END, z)

                ttk.Label(window, text="file load", font=LARGE_FONT, background="white").grid(row=1, column=0, padx=20, pady=10, sticky="w")

                a=

                for i in range(len(text_z)):
                frame = tk.Frame(window)
                frame.grid(row=i+2, column=0, sticky="nsew")
                ttk.Label(frame, text=text_z[i], background="white").grid(row=0, column=0, columnspan=3, padx=10, pady=2, sticky="w")
                a.append(ttk.Entry(frame, width=60, background="gray"))
                a[i].grid(row=1, column=0, columnspan=3, padx=10, sticky="ew")
                ttk.Button(frame, text="Search", width=10, command=lambda x=i: click(x)).grid(row=1, column=3, padx=5, sticky="w")

                def pring_current_paths():
                for ndex, entry in enumerate(a):
                print("Entry {}: ".format(ndex, entry.get()))

                tk.Button(window, text="Print gurrent paths!", command=pring_current_paths).grid()

                window.mainloop()





                share|improve this answer


























                  0












                  0








                  0







                  I think you should simplify things a bit by use a list to store your entry fields.
                  To do this I think it would be best to add frames for each set of widgets and to use the index of range to get what we need.



                  I have changed up your code a little to make it easier to work with list index as well as added a button that will print out each selected path on each entry field to show these values are accessible.



                  import tkinter as tk
                  from tkinter import ttk, filedialog

                  LARGE_FONT = ("Arial", 12)
                  MEDIUM_FONT = ("Arial", 11)
                  REGULAR_FONT = ("Arial", 10)

                  text_z = ["Select file 1", "Select the file 2", "Select file 3", "Select file 4"]

                  window = tk.Tk()

                  def click(x):
                  z = tk.filedialog.askopenfilename(initialdir="/", title="Select file", filetypes=(("Excel file", "*.xlsx"), ("All files", "*.*")))
                  a[x].insert(tk.END, z)

                  ttk.Label(window, text="file load", font=LARGE_FONT, background="white").grid(row=1, column=0, padx=20, pady=10, sticky="w")

                  a=

                  for i in range(len(text_z)):
                  frame = tk.Frame(window)
                  frame.grid(row=i+2, column=0, sticky="nsew")
                  ttk.Label(frame, text=text_z[i], background="white").grid(row=0, column=0, columnspan=3, padx=10, pady=2, sticky="w")
                  a.append(ttk.Entry(frame, width=60, background="gray"))
                  a[i].grid(row=1, column=0, columnspan=3, padx=10, sticky="ew")
                  ttk.Button(frame, text="Search", width=10, command=lambda x=i: click(x)).grid(row=1, column=3, padx=5, sticky="w")

                  def pring_current_paths():
                  for ndex, entry in enumerate(a):
                  print("Entry {}: ".format(ndex, entry.get()))

                  tk.Button(window, text="Print gurrent paths!", command=pring_current_paths).grid()

                  window.mainloop()





                  share|improve this answer













                  I think you should simplify things a bit by use a list to store your entry fields.
                  To do this I think it would be best to add frames for each set of widgets and to use the index of range to get what we need.



                  I have changed up your code a little to make it easier to work with list index as well as added a button that will print out each selected path on each entry field to show these values are accessible.



                  import tkinter as tk
                  from tkinter import ttk, filedialog

                  LARGE_FONT = ("Arial", 12)
                  MEDIUM_FONT = ("Arial", 11)
                  REGULAR_FONT = ("Arial", 10)

                  text_z = ["Select file 1", "Select the file 2", "Select file 3", "Select file 4"]

                  window = tk.Tk()

                  def click(x):
                  z = tk.filedialog.askopenfilename(initialdir="/", title="Select file", filetypes=(("Excel file", "*.xlsx"), ("All files", "*.*")))
                  a[x].insert(tk.END, z)

                  ttk.Label(window, text="file load", font=LARGE_FONT, background="white").grid(row=1, column=0, padx=20, pady=10, sticky="w")

                  a=

                  for i in range(len(text_z)):
                  frame = tk.Frame(window)
                  frame.grid(row=i+2, column=0, sticky="nsew")
                  ttk.Label(frame, text=text_z[i], background="white").grid(row=0, column=0, columnspan=3, padx=10, pady=2, sticky="w")
                  a.append(ttk.Entry(frame, width=60, background="gray"))
                  a[i].grid(row=1, column=0, columnspan=3, padx=10, sticky="ew")
                  ttk.Button(frame, text="Search", width=10, command=lambda x=i: click(x)).grid(row=1, column=3, padx=5, sticky="w")

                  def pring_current_paths():
                  for ndex, entry in enumerate(a):
                  print("Entry {}: ".format(ndex, entry.get()))

                  tk.Button(window, text="Print gurrent paths!", command=pring_current_paths).grid()

                  window.mainloop()






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 14 '18 at 13:50









                  Mike - SMTMike - SMT

                  9,55921434




                  9,55921434






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53300739%2ftkinter-filedialog-askopenfilename-iteration%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Florida Star v. B. J. F.

                      Error while running script in elastic search , gateway timeout

                      Adding quotations to stringified JSON object values