Python 如何让AskopenFileName在用户输入上循环?
我刚刚开始学习Python,我正试图在Python 如何让AskopenFileName在用户输入上循环?,python,python-3.x,tkinter,filedialog,Python,Python 3.x,Tkinter,Filedialog,我刚刚开始学习Python,我正试图在tkinter中实现这一点: 让用户在任何目录位置选择任意多个文件,并将其存储在另一个文件夹中 我不确定是否还有其他更有效的方法,但我已尝试通过以下方式解决此问题: 让用户输入他们正在搜索的文件数量(我将2设置为默认值) 循环显示文件数,并要求用户选择文件 将所有文件发送到新位置 问题是我不能很好地让存储文件和循环工作。代码如下: import tkinter as tk from tkinter import filedialog, ttk imp
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循环
将始终运行self.var.get()次
,如果用户在每次迭代
中选择两个(比如)文件,他将总共复制2*self.var.get()
,而且你一开始就要求复制确切数量的文件对我们没有任何好处
您的代码不起作用,因为您在每次迭代中都将文件列表
变量设置为新值列表(已选择文件)
(而不是将其追加到列表中)
解决方案2(可以选择多个文件-全部位于同一目录中)
这是推荐的,您将看到几乎在每个应用程序中都使用这种方法。在这里,您不需要询问用户希望复制多少文件。他们可以从单个文件到1000个文件(如果存在和需要)的任意位置进行选择,但文件应该位于相同的目录中。对于其他目录的文件,他们可以再次单击选择文件
按钮,按照流程操作
<>请注意,我没有删除<代码>标签>代码> <代码>条目< /代码>框,用于输入文件的数量,但请考虑它们从GUI中忽略。
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 = []
files_chosen = filedialog.askopenfilenames(title='Choose a file')
self.copy(list(files_chosen))
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循环
将始终运行self.var.get()次
,如果用户在每次迭代
中选择两个(比如)文件,他将总共复制2*self.var.get()
,而且你一开始就要求复制确切数量的文件对我们没有任何好处
您的代码不起作用,因为您在每次迭代中都将文件列表
变量设置为新值列表(已选择文件)
(而不是将其追加到列表中)
解决方案2(可以选择多个文件-全部位于同一目录中)
这是推荐的,您将看到几乎在每个应用程序中都使用这种方法。在这里,您不需要询问用户希望复制多少文件。他们可以从单个文件到1000个文件(如果存在和需要)的任意位置进行选择,但文件应该位于相同的目录中。对于其他目录的文件,他们可以再次单击选择文件
按钮,按照流程操作
<>请注意,我没有删除<代码>标签>代码> <代码>条目< /代码>框,用于输入文件的数量,但请考虑它们从GUI中忽略。
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 = []
files_chosen = filedialog.askopenfilenames(title='Choose a file')
self.copy(list(files_chosen))
def copy(self, file_list):
destination = filedialog.askdirectory()
for file in file_list:
shutil.copy(file, destination)
if __name__ == '__main__':
Getfiles()
所有文件(最初)是否都存储在同一个目录中(在将它们移动到新目录之前)?不一定是正确的。当它们在同一个目录中时,我有一个答案。现在编写答案,并将添加有关如何在不在sane目录下的情况下实现它的信息。所有文件(最初)是否存储在同一目录中(在将它们移动到新目录之前)?不必。是的,当它们在同一目录下时,我有一个案例的答案。现在写下答案,并在不在正常目录的情况下添加有关如何实现它的信息。@戴维怀特:没有问题:如果你解决了问题(或回答你的问题),请考虑投票或接受答案。“戴维怀特:没问题:”如果你解决了问题,考虑一下投票或接受答案。(或者回答了你的问题)。