Select多个目录并显示内容
Select multiple directories and display the contents
下面代码的目的是select一个目录。此代码的问题在于它不显示 selected 文件夹的内容。
示例:文件夹 A 有文件 B、C、D。
我希望使用 tkinter 将 B、C、D 的文件显示在文本框中。
from tkinter import *
from tkinter import filedialog
root = Tk()
root.geometry('200x200')
root.grid_rowconfigure(0, weight = 1)
root.grid_columnconfigure(0, weight = 1)
dirs = []
def get_directories():
dirs.append(filedialog.askdirectory())
return dirs
b1 = Button(root, text='select directories...', command = get_directories)
b1.pack()
root.mainloop()
我完全不熟悉使用 tkinter 和 python 编程。我希望我已经设法解释了我的问题。
我已经为你写了一个例子。如您所见,我的代码在 selected 文件夹中分别显示了文件和目录(如果不需要,您可以删除目录部分)。
代码:
from tkinter import *
from tkinter import filedialog
from os import listdir
from os.path import isfile, isdir, join
root = Tk()
root.geometry("200x200")
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
def get_directories():
selected_directory = filedialog.askdirectory()
print("Selected directory: {}".format(selected_directory))
content_of_selected_folder = listdir(selected_directory)
print(content_of_selected_folder)
for elem in content_of_selected_folder:
if isfile(join(selected_directory, elem)):
Label(root, text="File: {}".format(elem)).pack()
if isdir(join(selected_directory, elem)):
Label(root, text="Directory: {}".format(elem)).pack()
b1 = Button(root, text="select directories...", command=get_directories)
b1.pack()
root.mainloop()
测试输出:
>>> python3 test.py
Selected directory: /home/user/work_dir/tools/venv3
['include', 'lib', 'lib64', 'bin', 'pyvenv.cfg', 'pip-selfcheck.json']
界面:
如果要显示元素的完整路径:
for elem in content_of_selected_folder:
full_path_of_element = join(selected_directory, elem)
if isfile(full_path_of_element):
Label(root, text="File: {}".format(full_path_of_element)).pack()
if isdir(full_path_of_element):
Label(root, text="Directory: {}".format(full_path_of_element)).pack()
完整路径下的GUI:
注意:
根据你的问题标题(不知道是不是真的)你想显示多个目录的内容。如果你真的想这样做,那么你可以检查上面的代码片段。如果您多次点击 select directories...
按钮和 select 文件夹,则内容列表将使用新 selected 文件夹的内容进行扩展。
相关代码部分:
content_of_all_selected_dirs = []
def get_directories():
selected_directory = filedialog.askdirectory()
print("Selected directory: {}".format(selected_directory))
content_of_all_selected_dirs.extend(listdir(selected_directory))
print(content_of_all_selected_dirs)
for elem in content_of_all_selected_dirs:
full_path_of_element = join(selected_directory, elem)
if isfile(full_path_of_element):
Label(root, text="File: {}".format(full_path_of_element)).pack()
if isdir(full_path_of_element):
Label(root, text="Directory: {}".format(full_path_of_element)).pack()
selected /tmp/202108/20210817204008
目录后的 GUI:
selected /tmp/202109/20210909192402/resources
目录后的 GUI:
下面代码的目的是select一个目录。此代码的问题在于它不显示 selected 文件夹的内容。 示例:文件夹 A 有文件 B、C、D。 我希望使用 tkinter 将 B、C、D 的文件显示在文本框中。
from tkinter import *
from tkinter import filedialog
root = Tk()
root.geometry('200x200')
root.grid_rowconfigure(0, weight = 1)
root.grid_columnconfigure(0, weight = 1)
dirs = []
def get_directories():
dirs.append(filedialog.askdirectory())
return dirs
b1 = Button(root, text='select directories...', command = get_directories)
b1.pack()
root.mainloop()
我完全不熟悉使用 tkinter 和 python 编程。我希望我已经设法解释了我的问题。
我已经为你写了一个例子。如您所见,我的代码在 selected 文件夹中分别显示了文件和目录(如果不需要,您可以删除目录部分)。
代码:
from tkinter import *
from tkinter import filedialog
from os import listdir
from os.path import isfile, isdir, join
root = Tk()
root.geometry("200x200")
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
def get_directories():
selected_directory = filedialog.askdirectory()
print("Selected directory: {}".format(selected_directory))
content_of_selected_folder = listdir(selected_directory)
print(content_of_selected_folder)
for elem in content_of_selected_folder:
if isfile(join(selected_directory, elem)):
Label(root, text="File: {}".format(elem)).pack()
if isdir(join(selected_directory, elem)):
Label(root, text="Directory: {}".format(elem)).pack()
b1 = Button(root, text="select directories...", command=get_directories)
b1.pack()
root.mainloop()
测试输出:
>>> python3 test.py
Selected directory: /home/user/work_dir/tools/venv3
['include', 'lib', 'lib64', 'bin', 'pyvenv.cfg', 'pip-selfcheck.json']
界面:
如果要显示元素的完整路径:
for elem in content_of_selected_folder:
full_path_of_element = join(selected_directory, elem)
if isfile(full_path_of_element):
Label(root, text="File: {}".format(full_path_of_element)).pack()
if isdir(full_path_of_element):
Label(root, text="Directory: {}".format(full_path_of_element)).pack()
完整路径下的GUI:
注意:
根据你的问题标题(不知道是不是真的)你想显示多个目录的内容。如果你真的想这样做,那么你可以检查上面的代码片段。如果您多次点击 select directories...
按钮和 select 文件夹,则内容列表将使用新 selected 文件夹的内容进行扩展。
相关代码部分:
content_of_all_selected_dirs = []
def get_directories():
selected_directory = filedialog.askdirectory()
print("Selected directory: {}".format(selected_directory))
content_of_all_selected_dirs.extend(listdir(selected_directory))
print(content_of_all_selected_dirs)
for elem in content_of_all_selected_dirs:
full_path_of_element = join(selected_directory, elem)
if isfile(full_path_of_element):
Label(root, text="File: {}".format(full_path_of_element)).pack()
if isdir(full_path_of_element):
Label(root, text="Directory: {}".format(full_path_of_element)).pack()
selected /tmp/202108/20210817204008
目录后的 GUI:
selected /tmp/202109/20210909192402/resources
目录后的 GUI: