DearPyGui add_file_extension not working in file dialog after moving to object-oriented approach

175 Views Asked by At

I am working on a project using the DearPyGui library in Python, and I recently transitioned from a functional approach to an object-oriented approach. When I was using the functional approach, the add_file_extension function worked correctly in the file dialog, and the file extensions were applied as expected. However, after moving to the object-oriented approach, the add_file_extension function is not working as expected in the file dialog. When I open the file dialog, the file extensions I specified do not seem to be applied, and the filtering functionality is not working correctly.

Here is the relevant part of my code:

def main():
    file_explorer_instance = FileExplorerFilter()
    dpg.create_context()
    file_explorer_instance.file_explorer_filter()
    main_window(file_explorer_instance)
    # ... (setup DearPyGui and run the main loop)

def gcodeCombiner(file_explorer_filter=None):
    file_viewer_instance = FileViewerFrontEnd(file_explorer_filter)
    with dpg.group(horizontal=True):
        file_viewer_instance.file_viewer_window(view_width=400, view_height=700, button_height=25,
                   button_width=75, input_width=385, input_height=605)
        # ... (other UI elements and layout)

class FileExplorerFilter:
    def __init__(self):
        self.file_dialog_id = self.generate_unique_id(prefix="dialog_id")
        self.fileViewCallback = FileViewer()

    def generate_unique_id(self, prefix=""):
        return prefix + str(uuid.uuid4())

    def file_explorer_filter(self):
        with dpg.file_dialog(directory_selector=False, show=False, callback=self.fileViewCallback.callback_read_file, id=self.file_dialog_id, width=600, height=300):
            dpg.add_file_extension((".gcode",), color=(255, 0, 255, 255), custom_text="[Gcode]")
            dpg.add_file_extension(("",), color=(150, 255, 150, 255))

class FileViewerFrontEnd:
    def __init__(self, file_explorer_filter=None):  
        self.file_dialog_id = self.generate_unique_id(prefix="file_dialog_id")
        self.gcode_textbox_id = self.generate_unique_id(prefix="gcode_textbox")
        self.file_explorer_filter = file_explorer_filter or FileExplorerFilter()

    def generate_unique_id(self, prefix=""):
        return prefix + str(uuid.uuid4())

    def get_gcode_textbox_id(self):
        self.gcode_textbox_id

    def show_file_dialog(self, sender, app_data):
        dpg.show_item(self.file_explorer_filter.file_dialog_id)

    def file_viewer_window(self, view_width, view_height, button_width, button_height, input_width, input_height):
        with dpg.child_window(label="Add File Section", width=view_width, height=view_height):
            dpg.add_text("STEP 1:")
            dpg.add_button(label="Add File", width=button_width, height=button_height, callback=self.show_file_dialog)
            dpg.add_text("Gcode Input: ")
            dpg.add_input_text(default_value="No code to display", tag=self.gcode_textbox_id, multiline=True, readonly=True, width=input_width, height=input_height)

In the main function, I create an instance of FileExplorerFilter and call its file_explorer_filter method. The instance is then passed to the main_window function, which calls the gcodeCombiner function with the instance as an argument. Inside the gcodeCombiner function, I create an instance of FileViewerFrontEnd with the file_explorer_filter instance.

The file_explorer_filter method adds the file extensions to the file dialog, and the show_file_dialog method is called when the "Add File" button is clicked to open the file dialog.

I have followed the official DearPyGui documentation here, but I am still experiencing the issue. I have also tried updating the DearPyGui package to the latest version, but it did not solve the problem.

What could be the cause of this issue, and how can I fix it? Any help would be greatly appreciated.

Things I have tried so far:

  1. Initially, I used the add_file_extension function with single strings as arguments (e.g., ".gcode"), but it didn't work as expected after moving to an object-oriented approach.
  2. I then tried using tuples with single elements (e.g., (".gcode",)) as arguments for the add_file_extension function.
  3. I also checked whether the add_filter and filters methods were available in DearPyGui, but found out that they do not exist in the library.
  4. Lastly, I tried updating the DearPyGui package to the latest version, but this didn't solve the problem either.
0

There are 0 best solutions below