将目录中的多个文件夹名称添加到选项菜单 Python
Adding Multiple Folder Names From Directory To OptionMenu Python
我正在尝试将多个文件夹名称添加到选项菜单。下面的代码仅将一个文件夹名称添加到列表中,但我想添加目录中的所有文件夹名称。
var = StringVar()
os.chdir('C:\Users\mhoban')
all_subdirs = [d for d in os.listdir('.') if os.path.isdir(d)]
for dirs in all_subdirs:
dir = os.path.join('C:\Users\mhoban', dirs)
os.chdir(dir)
current = os.getcwd()
new = str(current).split("\")[3]
opt1 = OptionMenu(app, var, new)
opt1.grid(row=0, column=1, padx=10, pady=10)
opt1.configure(width = 40, bg = "White")
您需要构建一个菜单选项列表,然后将其解压到您现在经过 new
的位置:
options = []
for dirs in all_subdirs:
... # same
options.append(str(current).split("\")[3])
解包options
:
opt1 = OptionMenu(app, var, *options)
注意:options
将与 all_subdirs
相同,因此您的处理似乎没有实现任何目标。只需使用 all_subdirs
即可。
我正在尝试将多个文件夹名称添加到选项菜单。下面的代码仅将一个文件夹名称添加到列表中,但我想添加目录中的所有文件夹名称。
var = StringVar()
os.chdir('C:\Users\mhoban')
all_subdirs = [d for d in os.listdir('.') if os.path.isdir(d)]
for dirs in all_subdirs:
dir = os.path.join('C:\Users\mhoban', dirs)
os.chdir(dir)
current = os.getcwd()
new = str(current).split("\")[3]
opt1 = OptionMenu(app, var, new)
opt1.grid(row=0, column=1, padx=10, pady=10)
opt1.configure(width = 40, bg = "White")
您需要构建一个菜单选项列表,然后将其解压到您现在经过 new
的位置:
options = []
for dirs in all_subdirs:
... # same
options.append(str(current).split("\")[3])
解包options
:
opt1 = OptionMenu(app, var, *options)
注意:options
将与 all_subdirs
相同,因此您的处理似乎没有实现任何目标。只需使用 all_subdirs
即可。