文件和目录选择器¶
文件对话框项目可用于选择单个文件、多个文件或目录。当用户单击 Ok 按钮,则运行该对话框的回调。一个可选的第二个回调可以作为关键字参数提供,该回调在单击Cancel按钮时运行。
单击确定后,信息将通过APP_DATA参数传递,例如: * file path * 文件名 * current path * 当前过滤器(文件类型过滤器)
最简单的例子就是挑选董事。以下是示例
Code
import dearpygui.dearpygui as dpg
dpg.create_context()
def callback(sender, app_data):
print('OK was clicked.')
print("Sender: ", sender)
print("App Data: ", app_data)
def cancel_callback(sender, app_data):
print('Cancel was clicked.')
print("Sender: ", sender)
print("App Data: ", app_data)
dpg.add_file_dialog(
directory_selector=True, show=False, callback=callback, tag="file_dialog_id",
cancel_callback=cancel_callback, width=700 ,height=400)
with dpg.window(label="Tutorial", width=800, height=300):
dpg.add_button(label="Directory Selector", callback=lambda: dpg.show_item("file_dialog_id"))
dpg.create_viewport(title='Custom Title', width=800, height=600)
dpg.setup_dearpygui()
dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()
备注
如果未添加任何文件扩展名,则选择器默认为目录。
文件扩展名¶
文件扩展名是添加到文件对话框中的项目。您甚至可以设置文件扩展名的颜色。下面是一个简单的例子:
import dearpygui.dearpygui as dpg
dpg.create_context()
def callback(sender, app_data, user_data):
print("Sender: ", sender)
print("App Data: ", app_data)
with dpg.file_dialog(directory_selector=False, show=False, callback=callback, id="file_dialog_id", width=700 ,height=400):
dpg.add_file_extension(".*")
dpg.add_file_extension("", color=(150, 255, 150, 255))
dpg.add_file_extension("Source files (*.cpp *.h *.hpp){.cpp,.h,.hpp}", color=(0, 255, 255, 255))
dpg.add_file_extension(".h", color=(255, 0, 255, 255), custom_text="[header]")
dpg.add_file_extension(".py", color=(0, 255, 0, 255), custom_text="[Python]")
with dpg.window(label="Tutorial", width=800, height=300):
dpg.add_button(label="File Selector", callback=lambda: dpg.show_item("file_dialog_id"))
dpg.create_viewport(title='Custom Title', width=800, height=600)
dpg.setup_dearpygui()
dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()
定制化¶
可以使用面板定制文件对话框,只需将项添加到文件对话框中,就像它是常规容器一样。
这可以允许创建固定菜单、收藏夹、目录树等。
下面是一个示例:
import dearpygui.dearpygui as dpg
dpg.create_context()
def callback(sender, app_data):
print("Sender: ", sender)
print("App Data: ", app_data)
with dpg.file_dialog(directory_selector=False, show=False, callback=callback, tag="file_dialog_tag", width=700 ,height=400):
dpg.add_file_extension(".*")
dpg.add_file_extension("", color=(150, 255, 150, 255))
dpg.add_file_extension(".cpp", color=(255, 255, 0, 255))
dpg.add_file_extension(".h", color=(255, 0, 255, 255))
dpg.add_file_extension(".py", color=(0, 255, 0, 255))
with dpg.group(horizontal=True):
dpg.add_button(label="fancy file dialog")
dpg.add_button(label="file")
dpg.add_button(label="dialog")
dpg.add_date_picker()
with dpg.child_window(height=100):
dpg.add_selectable(label="bookmark 1")
dpg.add_selectable(label="bookmark 2")
dpg.add_selectable(label="bookmark 3")
with dpg.window(label="Tutorial", width=800, height=300):
dpg.add_button(label="File Selector", callback=lambda: dpg.show_item("file_dialog_tag"))
dpg.create_viewport(title='Custom Title', width=800, height=600)
dpg.setup_dearpygui()
dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()
选择多个文件¶
You can select multiple files by setting the file_count keyword Must use Ctrl + click to select multiple files Must use Shift + click to select multiple files
import dearpygui.dearpygui as dpg
dpg.create_context()
def callback(sender, app_data):
print("Sender: ", sender)
print("App Data: ", app_data)
with dpg.file_dialog(directory_selector=False, show=False, callback=callback, file_count=3, tag="file_dialog_tag", width=700 ,height=400):
dpg.add_file_extension("", color=(255, 150, 150, 255))
dpg.add_file_extension(".*")
dpg.add_file_extension(".cpp", color=(255, 255, 0, 255))
dpg.add_file_extension(".h", color=(255, 0, 255, 255))
dpg.add_file_extension(".py", color=(0, 255, 0, 255))
dpg.add_button(label="fancy file dialog")
with dpg.child_window(width=100):
dpg.add_selectable(label="bookmark 1")
dpg.add_selectable(label="bookmark 2")
dpg.add_selectable(label="bookmark 3")
with dpg.window(label="Tutorial", width=800, height=300):
dpg.add_button(label="File Selector", callback=lambda: dpg.show_item("file_dialog_tag"))
dpg.create_viewport(title='Custom Title', width=800, height=600)
dpg.setup_dearpygui()
dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()