我正在尝试在选择 perv 目录后添加一个新按钮来选择目录(以便选择多个目录)。
每次选择目录后都需要应用此选择目录按钮。我怎样才能做到这一点?
编辑: 在这里附上我的代码:
# Method to create a file picker
self.images_dir = FileChooseRow(
"Images Folder",
MetaFileType.DIRECTORY,
pick_files_dialogs[0],
fn_validate_submit,
)
def button_clicked(e):
self.add_images_dir.data += 1
self.row = ft.Row()
self.row.update()
# create button to add another file picker on screen on run time
self.add_images_dir = ft.IconButton(
icon="add", tooltip="Add", height=40, on_click=button_clicked, data=0
)
self.row = ft.Row()
我需要这样的东西:https://jsfiddle.net/SP8de/
有点不清楚你想要什么,但这是我的猜测:
我首先创建一个变量来存储添加的按钮和一行,其控件设置为数组。然后您可以将主按钮添加到其中。例如。
main_btn = ft.ElevatedButton(text="Chose directory")
added_btns = [main_btn]
r = ft.Row(added_btns)
page.add(r)
接下来,对主按钮进行回调
def add_button(e: ft.ControlEvent):
def dir_btn_pressed(e: ft.ControlEvent):
picker.ask_directory(initial_directory="...")
new_btn = ft.TextButton(text="Chose Directory")
new_btn.on_click=dir_btn_pressed
added_buttons.append(new_btn)
page.update()
main_btn.onclick = add_button
现在实现目录选择器:
def dir_picked(e: ft.FilePickerResultEvent):
print(f"Picked {e.path}")
picker = ft.FilePicker(on_result=dir_picked)
您可能需要修复奇怪的拼写错误:)