[英]Adding Multiple Folder Names From Directory To OptionMenu Python
I am trying to add multiple folder names to an options menu. 我想在选项菜单中添加多个文件夹名称。 The code below adds only one folder name to the list but I want to add all folder names in the directory.
下面的代码只为列表添加了一个文件夹名称,但我想在目录中添加所有文件夹名称。
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")
You need to build a list of menu options and then unpack it where you're passing new
at the moment: 您需要构建一个菜单选项列表,然后将其解压缩到您当前正在传递
new
位置:
options = []
for dirs in all_subdirs:
... # same
options.append(str(current).split("\\")[3])
Unpacking options
: 拆包
options
:
opt1 = OptionMenu(app, var, *options)
Note: options
will be the same as all_subdirs
, so your processing doesn't seem to achieve anything. 注意:
options
将与all_subdirs
相同,因此您的处理似乎无法实现。 Just use all_subdirs
instead. 只需使用
all_subdirs
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.