hoffstadt / DearPyGui

Dear PyGui: A fast and powerful Graphical User Interface Toolkit for Python with minimal dependencies
https://dearpygui.readthedocs.io/en/latest/
MIT License
13.28k stars 689 forks source link

Directory Selector duplicate folder name at the end of the string in "selections" dict #1942

Open nickosh opened 2 years ago

nickosh commented 2 years ago

Version of Dear PyGui

Version: 1.8.0 Operating System: Windows 11 (Build 22621.674)

My Issue/Question

When I used Directory Selector component its duplicate name of the folder at the end of string in "selections" dict. So, instead of "C:\test" folder it always will be "C:\test\test".

To Reproduce

Use dpg.add_file_dialog with directory_selector=True parameter.

Expected behavior

Folder path without duplication. If I choose "C:\test" I expect same path.

Screenshots/Video

dpg-fileselector-issue

Standalone, minimal, complete and verifiable example

import dearpygui.dearpygui as dpg

dpg.create_context()

with dpg.value_registry():
    dpg.add_string_value(default_value="", tag="dir_path")

def directory_picker_open(sender, data):
    selections = data["selections"]
    if len(selections) > 0:
        dir_path = selections[next(iter(selections))]
        print(str(dir_path))
        dpg.set_value("dir_path", dir_path)
    else:
        msg = "Folder not selected"
        print(msg)
        dpg.set_value("dir_path", msg)

def directory_picker_close(sender, data):
    print("Directory not selected")

dpg.add_file_dialog(
    directory_selector=True,
    file_count=1,
    show=False,
    callback=directory_picker_open,
    cancel_callback=directory_picker_close,
    tag="dialog_dir",
)

with dpg.window(tag="Main"):
    with dpg.child_window(autosize_x=True):
        with dpg.group():
            dpg.add_button(
                label="Select directory",
                callback=lambda: dpg.show_item("dialog_dir"),
            )
            with dpg.group(horizontal=True, horizontal_spacing=5):
                dpg.add_text("Directory Path: ")
                dpg.add_text(
                    source="dir_path",
                    color=[0, 255, 0],
                )

dpg.create_viewport(title="Test Directory Selector")
dpg.setup_dearpygui()
dpg.show_viewport()
dpg.set_primary_window("Main", True)
dpg.start_dearpygui()
dpg.destroy_context()
nickosh commented 2 years ago

Similar issue: https://github.com/hoffstadt/DearPyGui/issues/1257

Looks like mine issue have more information and recreation way so I hope it will be more useful for fixing.

LiyueAnd commented 4 months ago

I have also found a similar issue. My method of operation is to press Ctrl to select two directories, but the returned path is incorrect and must be processed twice before it can be used

W-Wuxian commented 2 months ago

Hi a quick fix would be to use app_data["selections"].items() and a custom function remove_penultimate_to_last() inside your directory_selector callback function as follow:

import dearpygui.dearpygui as dpg
import os
import re

def remove_penultimate_to_last(text, pattern):
   idx = text.rfind(pattern, 0, text.rfind(pattern))
   return text[0:idx]

dpg.create_context()

def callback(sender, app_data):
    print('OK was clicked.')
    print("Sender: ", sender)
    print("App Data: ", app_data)
    print("\n")
    for key, val in app_data["selections"].items():
       print(" :: ", remove_penultimate_to_last(val,"/")+"/"+key)

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()