下拉文件列表依赖于另一个下拉文件夹列表
Dropdown files list dependent from another dropdown folder list
我需要有关如何从另一个下拉文件夹列表中创建依赖下拉文件列表的帮助。
在一个文件夹中,我有 5 个文件夹列表,然后对于每个文件夹,我有 5 个文件列表。在我的代码中,我已经制作了 2 个下拉功能,即第一个下拉菜单:列出所有文件夹。第二个下拉列表:我想在第二个下拉列表中创建一个依赖列表,这取决于我在第一个下拉列表中 select 的文件夹。这可能吗?
例如:
目录:'C:\Users\TestFile'
文件夹列表:
-文件夹1
-文件夹2
-文件夹3
-文件夹4
-文件夹5
每个文件夹中的文件列表:
folder1 : apple1,apple2,apple3,.....
folder2 : orange1,orange2,orange3,.....
文件夹 3:葡萄 1、葡萄 2、葡萄 3、.....
folder4 : lych1,lych2,lych3,.....
folder5 : strwberry1,strwberry2,strwberry3,.....
因此,如果我 select folder1 在我的第一个下拉列表中,我想要在第二个下拉列表中列出的 folder1 中的所有文件(apple1、apple2、apple3、.....)。
这是我的代码
from tkinter import *
from functools import partial
import os
import tkinter as tk
from tkinter import ttk
#import tkinter as tk
from tkinter import filedialog, messagebox, ttk
import pandas as pd
import webbrowser
from webbrowser import open as openlink
import urllib
def open(file_menu):
filename = file_menu.get()
open_in_browser_link = f"file://{os.path.join(folder, filename)}"
print(open_in_browser_link)
webbrowser.open(open_in_browser_link)
def clear_data():
# tv1.delete(*tv1.get_children())
return None
folder = os.path.realpath(r'C:\Users\TestFile')
filelist = [fname for fname in os.listdir(folder)]
master = tk.Tk()
master.geometry('1200x800')
master.title('THB')
# Frame for TreeView
frame0 = tk.LabelFrame(master, text="Chapter",background="light grey")
frame0.place(height=500, width=1200, rely=0.0, relx=0.0)
optmenu = ttk.Combobox(frame0, values=filelist, state='readonly')
optmenu.pack()
optmenu.set("Chapter")
optmenu1 = ttk.Combobox(frame0, values=????, state='readonly')
optmenu1.pack()
optmenu1.set("SubChapter")
button_select = tk.Button(frame0, text="Show Data",
width=15,
height=2,
compound=tk.CENTER,
command=partial(open, optmenu1))
button_select.place(relx=0.5, rely=0.5)
button_select.pack(side=tk.TOP)
master.mainloop()
代码want背后的基本算法可以拆分如下:
- 将第一个组合框的每个选项 select 信号绑定到一个函数,
- 扫描目录并将可用文件设置到第二个组合框。
现在,此操作的重要部分是:
optmenu.bind("<<ComboboxSelected>>", onSelect)
...
...
def onSelect(event):
folder_name = filelist[event.widget.current()]
folder_path = os.path.join(folder, folder_name)
print(f"folder selected:{folder_name}, folder_path:{folder_path}, index:{event.widget.current()}")
global sub_files
sub_files = []
for f_name in os.listdir(folder_path):
sub_files.append(f_name)
print(sub_files)
optmenu1['values'] = sub_files # set sub directories to combobox
完整代码:
from tkinter import *
from functools import partial
import os
import tkinter as tk
from tkinter import ttk
# import tkinter as tk
from tkinter import filedialog, messagebox, ttk
import pandas as pd
import webbrowser
from webbrowser import open as openlink
import urllib
def open(file_menu):
filename = file_menu.get()
open_in_browser_link = f"file://{os.path.join(folder, filename)}"
print(open_in_browser_link)
webbrowser.open(open_in_browser_link)
def clear_data():
# tv1.delete(*tv1.get_children())
return None
def onSelect(event):
folder_name = filelist[event.widget.current()]
folder_path = os.path.join(folder, folder_name)
print(f"folder selected:{folder_name}, folder_path:{folder_path}, index:{event.widget.current()}")
global sub_files
sub_files = []
for f_name in os.listdir(folder_path):
sub_files.append(f_name)
print(sub_files)
optmenu1['values'] = sub_files # set sub directories to combobox
folder = os.path.realpath(r'C:\Users\TestFile')
sub_files = []
# folder = os.path.realpath(r'./testing')
filelist = [fname for fname in os.listdir(folder)]
master = tk.Tk()
master.geometry('1200x800')
master.title('THB')
# Frame for TreeView
frame0 = tk.LabelFrame(master, text="Chapter", background="light grey")
frame0.place(height=500, width=1200, rely=0.0, relx=0.0)
optmenu = ttk.Combobox(frame0, values=filelist, state='readonly')
optmenu.pack()
optmenu.set("Chapter")
optmenu.bind("<<ComboboxSelected>>", onSelect)
optmenu1 = ttk.Combobox(frame0, values=[], state='readonly')
optmenu1.pack()
optmenu1.set("SubChapter")
button_select = tk.Button(frame0, text="Show Data",
width=15,
height=2,
compound=tk.CENTER,
command=partial(open, optmenu1))
button_select.place(relx=0.5, rely=0.5)
button_select.pack(side=tk.TOP)
master.mainloop()
现在,此代码打开 C:\Users\TestFile
文件夹,扫描每个目录并设置为第一个组合框 (optmenu
)。
然后,当您select optmenu
中的任何一个选项时,它会调用onSelect()
函数。此 onSelect
函数读取 select 文件夹中所有可用的文件 optmenu
,并显示 optmenu1
.
中的可用文件
我需要有关如何从另一个下拉文件夹列表中创建依赖下拉文件列表的帮助。 在一个文件夹中,我有 5 个文件夹列表,然后对于每个文件夹,我有 5 个文件列表。在我的代码中,我已经制作了 2 个下拉功能,即第一个下拉菜单:列出所有文件夹。第二个下拉列表:我想在第二个下拉列表中创建一个依赖列表,这取决于我在第一个下拉列表中 select 的文件夹。这可能吗?
例如:
目录:'C:\Users\TestFile'
文件夹列表: -文件夹1 -文件夹2 -文件夹3 -文件夹4 -文件夹5
每个文件夹中的文件列表: folder1 : apple1,apple2,apple3,..... folder2 : orange1,orange2,orange3,..... 文件夹 3:葡萄 1、葡萄 2、葡萄 3、..... folder4 : lych1,lych2,lych3,..... folder5 : strwberry1,strwberry2,strwberry3,.....
因此,如果我 select folder1 在我的第一个下拉列表中,我想要在第二个下拉列表中列出的 folder1 中的所有文件(apple1、apple2、apple3、.....)。
这是我的代码
from tkinter import *
from functools import partial
import os
import tkinter as tk
from tkinter import ttk
#import tkinter as tk
from tkinter import filedialog, messagebox, ttk
import pandas as pd
import webbrowser
from webbrowser import open as openlink
import urllib
def open(file_menu):
filename = file_menu.get()
open_in_browser_link = f"file://{os.path.join(folder, filename)}"
print(open_in_browser_link)
webbrowser.open(open_in_browser_link)
def clear_data():
# tv1.delete(*tv1.get_children())
return None
folder = os.path.realpath(r'C:\Users\TestFile')
filelist = [fname for fname in os.listdir(folder)]
master = tk.Tk()
master.geometry('1200x800')
master.title('THB')
# Frame for TreeView
frame0 = tk.LabelFrame(master, text="Chapter",background="light grey")
frame0.place(height=500, width=1200, rely=0.0, relx=0.0)
optmenu = ttk.Combobox(frame0, values=filelist, state='readonly')
optmenu.pack()
optmenu.set("Chapter")
optmenu1 = ttk.Combobox(frame0, values=????, state='readonly')
optmenu1.pack()
optmenu1.set("SubChapter")
button_select = tk.Button(frame0, text="Show Data",
width=15,
height=2,
compound=tk.CENTER,
command=partial(open, optmenu1))
button_select.place(relx=0.5, rely=0.5)
button_select.pack(side=tk.TOP)
master.mainloop()
代码want背后的基本算法可以拆分如下:
- 将第一个组合框的每个选项 select 信号绑定到一个函数,
- 扫描目录并将可用文件设置到第二个组合框。
现在,此操作的重要部分是:
optmenu.bind("<<ComboboxSelected>>", onSelect)
...
...
def onSelect(event):
folder_name = filelist[event.widget.current()]
folder_path = os.path.join(folder, folder_name)
print(f"folder selected:{folder_name}, folder_path:{folder_path}, index:{event.widget.current()}")
global sub_files
sub_files = []
for f_name in os.listdir(folder_path):
sub_files.append(f_name)
print(sub_files)
optmenu1['values'] = sub_files # set sub directories to combobox
完整代码:
from tkinter import *
from functools import partial
import os
import tkinter as tk
from tkinter import ttk
# import tkinter as tk
from tkinter import filedialog, messagebox, ttk
import pandas as pd
import webbrowser
from webbrowser import open as openlink
import urllib
def open(file_menu):
filename = file_menu.get()
open_in_browser_link = f"file://{os.path.join(folder, filename)}"
print(open_in_browser_link)
webbrowser.open(open_in_browser_link)
def clear_data():
# tv1.delete(*tv1.get_children())
return None
def onSelect(event):
folder_name = filelist[event.widget.current()]
folder_path = os.path.join(folder, folder_name)
print(f"folder selected:{folder_name}, folder_path:{folder_path}, index:{event.widget.current()}")
global sub_files
sub_files = []
for f_name in os.listdir(folder_path):
sub_files.append(f_name)
print(sub_files)
optmenu1['values'] = sub_files # set sub directories to combobox
folder = os.path.realpath(r'C:\Users\TestFile')
sub_files = []
# folder = os.path.realpath(r'./testing')
filelist = [fname for fname in os.listdir(folder)]
master = tk.Tk()
master.geometry('1200x800')
master.title('THB')
# Frame for TreeView
frame0 = tk.LabelFrame(master, text="Chapter", background="light grey")
frame0.place(height=500, width=1200, rely=0.0, relx=0.0)
optmenu = ttk.Combobox(frame0, values=filelist, state='readonly')
optmenu.pack()
optmenu.set("Chapter")
optmenu.bind("<<ComboboxSelected>>", onSelect)
optmenu1 = ttk.Combobox(frame0, values=[], state='readonly')
optmenu1.pack()
optmenu1.set("SubChapter")
button_select = tk.Button(frame0, text="Show Data",
width=15,
height=2,
compound=tk.CENTER,
command=partial(open, optmenu1))
button_select.place(relx=0.5, rely=0.5)
button_select.pack(side=tk.TOP)
master.mainloop()
现在,此代码打开 C:\Users\TestFile
文件夹,扫描每个目录并设置为第一个组合框 (optmenu
)。
然后,当您select optmenu
中的任何一个选项时,它会调用onSelect()
函数。此 onSelect
函数读取 select 文件夹中所有可用的文件 optmenu
,并显示 optmenu1
.