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
12.6k stars 667 forks source link

SystemError with dpg.file_dialog #2324

Open whitews opened 2 months ago

whitews commented 2 months ago

DearPyGUI version: 1.11.1 Python version: 3.10 Operating System: Ubuntu 22.04

I'm seeing the same error as in discussion #1609 using the file_dialog. Using the code below, the error occurs on subsequent launches of the file dialog (not on the first launch). The error message made me suspect the issue related to using a tag for the file_dialog. Indeed, when remove the tag from the code, the error is avoided. Maybe using a tag for a dialog is not recommended, but I got in the habit of tagging all UI elements. In any case, hope this helps narrow down the bug.

To Reproduce

Steps to reproduce the behavior:

  1. Launch the MRE code included below
  2. Click on the 'Load' button.
  3. Select a file or cancel, doesn't matter. Just get the dialog to go away.
  4. Click the 'Load' button a 2nd time.
  5. Kablamo! Error occurs

Expected behavior

I expect no kablamos using the file dialog.

Standalone, minimal, complete and verifiable example

import dearpygui.dearpygui as dpg

def choose_file():
    with dpg.file_dialog(
            tag='file_selector',  # <-- causes SystemError
            width=700, 
            height=500,
            show=True
    ):
        dpg.add_file_extension(".*", color=(31, 31, 127, 255))

dpg.create_context()
dpg.create_viewport(title='File dialog crash app', width=900, height=600)

with dpg.window(tag="main_win", no_title_bar=True):
    dpg.add_button(label="Load", callback=choose_file)

dpg.setup_dearpygui()
dpg.show_viewport()
dpg.set_primary_window('main_win', True)
dpg.start_dearpygui()
dpg.destroy_context()

Traceback

Traceback (most recent call last):
  File "...venv/lib/python3.10/site-packages/dearpygui/dearpygui.py", line 1816, in file_dialog
    widget = internal_dpg.add_file_dialog(label=label, user_data=user_data, use_internal_label=use_internal_label, tag=tag, width=width, height=height, callback=callback, show=show, default_path=default_path, default_filename=default_filename, file_count=file_count, modal=modal, directory_selector=directory_selector, min_size=min_size, max_size=max_size, cancel_callback=cancel_callback, **kwargs)
SystemError: <built-in function add_file_dialog> returned a result with an exception set

During handling of the above exception, another exception occurred:

Exception: Error: [1009] Message:   No container to pop.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "...debug_file_dialog_crash.py", line 5, in choose_file
    with dpg.file_dialog(
  File "/usr/lib/python3.10/contextlib.py", line 135, in __enter__
    return next(self.gen)
  File "...venv/lib/python3.10/site-packages/dearpygui/dearpygui.py", line 1820, in file_dialog
    internal_dpg.pop_container_stack()
SystemError: <built-in function pop_container_stack> returned a result with an exception set
v-ein commented 2 months ago

The file dialog behaves much like a regular dpg.window (it's a window, after all). In particular, when you click OK or Cancel, it doesn't get deleted - instead, it gets hidden. This way you can query some properties from it or reuse the dialog widget later.

You can see it by adding the following line to your example:

dpg.show_item_registry()

Look at the File Dialogs section in the Item Registry. image You can even bring it back by clicking the Show button in the Item Registry.

So when you click the Load button in your examlpe second time, there's already a file dialog in the GUI with the tag "file_selector". DPG actually tells you that straight, before the first SystemError message:

Exception:
Error:     [1000]
Command:   add alias
Item:      0
Label:     Not found
Item Type: Unknown
Message:   Alias already exists

The same would happen if you were creating a dpg.window that way. When the window is closed, it remains hidden until you delete it explicitly with dpg.delete_item().

It is perfectly normal to use a tag with dpg.file_dialog, as long as your code manages the item lifetime properly:

whitews commented 2 months ago

Hi Vladimir,

Thanks for the explanation. That logic and message is what made me try removing the tag to see if it fixes the problem. I get that is how it works, but the documentation doesn't mention this and the example there uses a tag without managing the dialog.

In any other GUI library I've used, I don't recall dialog windows having to managed this way. Just my opinion, but this feels like an implementation detail that should be handled in the library itself. It is nice that it would be an option to keep an instance of the dialog and show / hide it manually, but again, maybe something that should be handled automatically by default.

Also, I found in debug mode it crashes the whole program. In normal running mode, it emits the error I posted but the app remains open and an additional file dialog is opened (if the first one wasn't closed).