如何解决如何获取askopenfilenames来遍历用户输入?
我刚刚开始学习Python,我正尝试在tkinter
中实现这一目标:
- 让用户在任何目录位置选择多个文件并将其存储在另一个文件夹中
我不确定是否还有其他更有效的方法,但是我尝试通过以下方式解决此问题:
- 让用户输入要搜索的文件数量(我将默认值设置为2)
- 循环查看文件数量,并要求用户选择文件
- 将所有文件发送到新位置
问题是我无法完全存储文件并循环工作。这是代码:
import tkinter as tk
from tkinter import filedialog,ttk
import shutil
class Getfiles():
def __init__(self):
# initialising the screen name,size title and icon
root = tk.Tk()
root.geometry('450x100')
root.resizable(0,0)
root.configure(bg='#002060')
# initialising integer value to pass to select_files method
self.var = tk.IntVar()
self.var.set('2')
# Initialising the frame to insert our widget
frame_three = tk.Frame(root,bg='#002060')
frame_three.pack(side='top',fill='both')
# setting label to tell user to input no. of files
num_label = ttk.Label(frame_three,text='No. of files: ',background='#002060',foreground='white')
num_label.pack(side='left',padx=(40,10),pady=(20,20))
# setting number of files user wants to fetch
files_num = ttk.Entry(frame_three,width=3,textvariable=self.var)
files_num.pack(side='left',padx=(10,40),pady=20)
# get user to select the files listed
select_button = ttk.Button(frame_three,text='Select files',width=30,command=self.select_files)
select_button.pack(side='left',padx=(50,10))
root.mainloop()
def select_files(self):
file_list = []
for i in range(self.var.get()):
file_chosen = filedialog.askopenfilenames(title='Choose a file')
file_list = list(file_chosen)
list += file_list
self.copy(file_list)
def copy(self,file_list):
destination = filedialog.askdirectory()
for file in file_list:
shutil.copy(file,destination)
if __name__ == '__main__':
Getfiles()
解决方法
解决方案1(根据您的需要-文件可以位于不同的位置)
注意事项:我将filedialog.askopenfilenames()
更改为filedialog.askopenfilename()
,因为如果用户应该整体复制确切数量的文件(由self.var
中的用户输入定义),在这种情况下,允许用户一次选择多个文件可能会导致超出self.var
的值,因为我们的for loop
将始终运行self.var.get() times
,并且如果用户选择了两个(例如)文件,每个iteration
,他最终都会总共复制2*self.var.get()
个文件,而您一开始就要求要复制的确切文件数的想法对我们没有任何帮助。
您的代码无法正常工作,因为您是在每个file_list
上将list(files_chosen)
变量设置为新值iteration
(而不是将appending
设置到列表中)>
import tkinter as tk
from tkinter import filedialog,ttk
import shutil
class Getfiles():
def __init__(self):
# initialising the screen name,size title and icon
root = tk.Tk()
root.geometry('450x100')
root.resizable(0,0)
root.configure(bg='#002060')
# initialising integer value to pass to select_files method
self.var = tk.IntVar()
self.var.set('2')
# Initialising the frame to insert our widget
frame_three = tk.Frame(root,bg='#002060')
frame_three.pack(side='top',fill='both')
# setting label to tell user to input no. of files
num_label = ttk.Label(frame_three,text='No. of files: ',background='#002060',foreground='white')
num_label.pack(side='left',padx=(40,10),pady=(20,20))
# setting number of files user wants to fetch
files_num = ttk.Entry(frame_three,width=3,textvariable=self.var)
files_num.pack(side='left',padx=(10,40),pady=20)
# get user to select the files listed
select_button = ttk.Button(frame_three,text='Select files',width=30,command=self.select_files)
select_button.pack(side='left',padx=(50,10))
root.mainloop()
def select_files(self):
files_list = []
for x in range(self.var.get()):
files_chosen = filedialog.askopenfilename(title='Choose a file')
files_list += list(files_chosen)
self.copy(files_list)
def copy(self,file_list):
destination = filedialog.askdirectory()
for file in file_list:
shutil.copy(file,destination)
if __name__ == '__main__':
Getfiles()
solution2(可以选择多个文件-全部都在同一目录中)
建议这样做,您将在几乎所有应用程序中看到此方法。在这里,您无需询问用户他们希望复制多少文件。他们可以从单个文件到1000个文件(如果存在和需要)中的任意一个进行选择,但是文件应该位于同一directory
中。对于其他目录的文件,他们可以再次单击select files
按钮,请按照以下过程进行操作。
请注意,我并未删除用于输入文件数量的label
和entry
框,但认为它们已从GUI中忽略。
import tkinter as tk
from tkinter import filedialog,10))
root.mainloop()
def select_files(self):
files_list = []
files_chosen = filedialog.askopenfilenames(title='Choose a file')
self.copy(list(files_chosen))
def copy(self,destination)
if __name__ == '__main__':
Getfiles()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。