Tkinter Filedialog.askopenfilename 迭代

Tkinter Filedialog.askopenfilename iteration

我正在开发程序来加载文件并使用这些加载的文件执行一些计算。

为此,我编写了一个简单的迭代代码来加载 tkinter 变量。 window、标签、条目和按钮位置已经完成。到目前为止,我的代码是:

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()

我的问题出在点击按钮上。它应该在单击 运行 askopenfilename 期间获取文件路径并显示在输入框上,但所有按钮都将其指向最后创建的输入框。

有人可以帮我解决这个问题吗?

非常感谢!

Lambda 救援。需要知道要更新的正确 Button-Entry 对。所以按下按钮时传递对应索引的值。

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()

我认为您应该通过使用列表来存储您的输入字段来简化一些事情。 为此,我认为最好为每组小部件添加框架并使用范围索引来获取我们需要的内容。

我对您的代码进行了一些更改,以便更轻松地使用列表索引,并添加了一个按钮,该按钮将在每个输入字段上打印出每个选定的路径,以显示这些值是可访问的。

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()