简体   繁体   English

从目录添加多个文件夹名称到OptionMenu Python

[英]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.

相关问题 目录中的 Python 文件夹名称 - Python folder names in the directory 如何从目录中的文件夹名称中提取后缀字符串? (蟒蛇) - How to Extract A String of the Suffix from Folder Names in a Directory? (Python) 如何从基于 python 文件夹名称列表的目录中获取 select 文件夹? - How to select folders from a directory based on a python list of the folder names? Python - 添加文件名(非完整路径)以从目录和子文件夹中列出 - Python - adding file names (not full paths) to list from directory and subfolders Python,easyGUI和sqlite3:使用python读取一个文件夹或多个文件夹中的文件,然后将文件夹名称和文件名添加到数据库 - Python, easyGUI and sqlite3: Using python to read files in one folder or multiple folder, then adding the folder names and filenames to database 从python中的文件夹中读取多个.txt文件的名称 - Reading names of multiple .txt files from a folder in python Python - 仅列出文件夹名称而不是完整目录 - Python - Only listing folder names and not full directory 使用 python 重命名目录中的多个文件夹 - Rename Multiple folder in a directory with python Python Tkinter OptionMenu向多个OptionMenus添加命令 - Python Tkinter OptionMenu add a command to multiple OptionMenus python:从不同文件目录导入模块,文件夹名称带有空格 - python: import module from different file directory, with folder names having spaces
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM