[英]How to get askopenfilenames to loop over a user input?
我剛剛開始學習 Python,我正在嘗試在tkinter
實現這一目標:
我不確定是否還有其他更有效的方法可以做到這一點,但我嘗試通過以下方式解決這個問題:
問題是我無法讓存儲文件和循環正常工作。 這是代碼:
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()
注意事項:我將filedialog.askopenfilenames()
更改為filedialog.askopenfilename()
因為如果用戶應該復制整個文件的確切數量(由用戶在self.var
輸入定義),在這種情況下,允許用戶選擇一次多個文件可能會導致超過self.var
的值,因為我們的for loop
將始終運行self.var.get() times
,如果用戶在每次iteration
選擇兩個(比如說)文件,他將最終總共復制2*self.var.get()
文件,並且您在開始時要求復制文件的確切數量的想法對我們沒有任何好處。
您的代碼不起作用,因為您在每次iteration
都將file_list
變量設置為新值list(files_chosen)
(而不是appending
其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()
這是推薦的,您會看到這種方法幾乎用於每個應用程序。 在這里,您無需詢問用戶他們希望復制多少個文件。 他們可以選擇從單個文件到 1000 個文件(如果存在和需要)的任何位置,但文件應該位於同一directory
。 對於其他目錄的文件,他們可以再次單擊select files
按鈕,按照流程操作。
請注意,我沒有刪除用於輸入文件數量的label
和entry
框,而是認為它們從 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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.