-1

I had tried to create an interactive tool that takes features selected in the active ArcMap view, allows user to pick a file and then attach it to selected features.

However, everything ends at tkFileDialog.askopenfilename() method crashing ArcMap. I have 10.5.1, but I managed to crash ArcMap in 10.7.1 on my colleagues machine.

Is that issue known, is that Python issue or ArcMap-s? How to deal with that?

My example is here (crashes ArcMap): https://github.com/r-pankevicius/arcgis-addin-tool-py-template-with-selection-input

Crashing code in Install/AddInWithSelectionInput_addin.py:

@staticmethod
def PickFileToAttach():
    root = tkinter.Tk()
    root.withdraw()
    options = {
        'parent': root,
        'title': "Select a file to attach",
        'multiple': False,
        'filetypes': [('All files', '*')]
    }
    pathFile = tkFileDialog.askopenfilename(**options)
    root.destroy()
    return pathFile

And I am not 100% convinced that "custom toolbox scripts that are run "in-process" will always cause crashes", because if I play with actually the same code in Python window in ArcMap it goeas quite well without a crash:

>>> 
... import Tkinter as tkinter
... import tkFileDialog
... 
>>> def PickFileToAttach():
...     root = tkinter.Tk()
...     root.withdraw()
...     options = {
...         'parent': root,
...         'title': "Select a file to attach",
...         'multiple': False,
...         'filetypes': [('All files', '*')]
...     }
...     pathFile = tkFileDialog.askopenfilename(**options)
...     root.destroy()
...     return pathFile
...     
>>> PickFileToAttach()
u'C:/Users/***/Documents/ArcGIS-extensions.docx'
>>> PickFileToAttach()
''
>>> PickFileToAttach()
''
>>> 
  • @user2856 I have read a discussion and am not sure why I can't use Tkinter in ArcMap. I see the same issue here gis.stackovernet.com/fr/q/39278 – Remigijus Pankevičius Nov 17 '19 at 15:38
  • There is a book "Python for ArcGIS" that mentions use of tkFileDialog.askopenfilename books.google.lt/… – Remigijus Pankevičius Nov 17 '19 at 15:57
  • Well, then there is a small problem for me to solve... If I'll make .NET addin instead then I'll be investing in ArcObjects which will go to dust after migrating to ArcGIS Pro. – Remigijus Pankevičius Nov 18 '19 at 7:40
  • 1
    You might be better off creating a Python toolbox or script tool as ArcGIS Pro doesn't support Python addins either. That will solve your file dialog issue too, you can just specify a parameter as a file (DEFile in a Python Toolbox) and then ArcGIS (ArcMap & Pro) will create the file dialog for you. – user2856 Nov 18 '19 at 8:13
-1

I've got an idea what's going on here and I tested it and Bingo!

I started ArcMap making sure Geoprocessing Python window was not opened. My addin running that problematic tkFileDialog.askopenfilename() code worked well, no crashes. Checked several times.

Then I opened Geoprocessing Python window and tested later code in it (in Python window), it worked too.

But now after I press my addin button - ArcMap crashes.

In short: addin with Python GUI works in case you don't open Python window in ArcMap.

So what's going on here? To integrate Tkinter GUI Esri had to sync it's message loop with main application (ArcMap) message loop. And there are 2 pieces of code in ArcMap that does it well, separately... But the issue is that one piece of code in Geoprocessing Python window implementation just overrides data structures (memory) that another piece of code has set up (most likely vtable pointers got destroyed).

My conclusion: there is a bug in ArcMap that was not fixed for years and probably will never be fixed (due to push of ArcGIS Pro). Therefore I can find diverse results of some people telling it's possible to have Python GUI in ArcMap while others tell "Don't! You may crash ArcMap!".

IMHO by not fixing this bug for a long while esri missed a pile of good examples of mix of Python UI and geoprocessing made by data scientists and GIS enthusiasts.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.