Is it possible to script a Dialog box?

Hi All,

I have a script that would work much better if I could pick a file after starting the script, however when I tried to use Tkinter in the most basic way, the script threw an error. See the code I used and it’s reference below.

I assume this mean that we cannot have dialog boxes when using Python4Capella?

openfiledialog - Quick and easy file dialog in Python? - Stack Overflow

import tkinter as tk
from tkinter import filedialog

root = tk.Tk()
root.withdraw()

file_path = filedialog.askopenfilename()

Hi,

I did this some times ago on a previous version of Python 4 Capella using PySimpleGUI
I do not know if it still works on newer versions of Python 4 Capella.
Sorry for this “pointer only” answer.

Regards,
Arnaud

Thanks for the pointer Arnaud,
Unfortunately when I tried the basic example I received the same error as I did for Tkinter. It seems PySimpleGUI is built on tkinter:

org.eclipse.ease.ScriptExecutionException: Traceback (most recent call last):
  File "workspace://Python4Capella/RESAN_0.3/test2.py", line 1, in <module>
    ###############################################################################
    ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\xxxxx\site-packages\PySimpleGUI\__init__.py", line 1, in <module>
    from .PySimpleGUI import *
  File "C:\xxxxx\site-packages\PySimpleGUI\PySimpleGUI.py", line 93, in <module>
    tclversion_detailed = tkinter.Tcl().eval('info patchlevel')
                          ^^^^^^^^^^^^^
  File "C:\xxxxx\Lib\tkinter\__init__.py", line 2428, in Tcl
    return Tk(screenName, baseName, className, useTk)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\xxxxx\Lib\tkinter\__init__.py", line 2321, in __init__
    baseName = os.path.basename(sys.argv[0])
                                ~~~~~~~~^^^
  File "C:\xxxxx\capella\plugins\py4j-python_0.10.9.5-bnd-2odeag\src\py4j\java_collections.py", line 201, in __getitem__
    return self.__compute_item(key)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\xxxxx\capella\plugins\py4j-python_0.10.9.5-bnd-2odeag\src\py4j\java_collections.py", line 177, in __compute_item
    new_key = self.__compute_index(key)
              ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\xxxxx\capella\plugins\py4j-python_0.10.9.5-bnd-2odeag\src\py4j\java_collections.py", line 174, in __compute_index
    raise IndexError("list index out of range")
IndexError: list index out of range

So now I am wondering if this is a bug?

Hi Tim,

Python4Capella use EASE to run the Python code in the context of Capella. This probably the root of your problem, but EASE also come with ways to create GUIs. You can have a look to the tips and tricks for creating and contributing to Capella GUI.

To open a file dialog you can use the following code:

file = showFileSelectionDialog("/tmp", 1, "Open a file", "this open a file for...")            
print(file)

To list functions provided by EASE modules you can use the module explorer view (use the menu Window/Show view/Other… then module explorer).

You should see the following view:

Some environment variables are not defined when running a Python script from EASE. One way of dealing with this specific issue could be to define sys.argv. But it might not be enough to make your script run.

Hi Yvan,

Thank you, this has worked perfectly.

I therefore now understand that we must respect the EASE framework and cannot use other libraries to build dialogs. Please correct me if that is not the case.

I guess it could be possible to use other libraries, but you might need to initialize system variables that are expected to exist in the Python environment. That said I don’t know what effort would be needed. It probably highly depend on the library. But at some point if there is a need for a common set of initialization we can probably do add it in the context of Python4Capella or maybe contribute that to EASE.

I opened an issue to track this: