1

I would like to create a dialog box which pops up when the user click on a button from a Python Add-In toolbar.

The user will navigate to their preferred file and select any shapefiles they wish to add to ArcMap.

I found this code:

    import Tkinter, tkFileDialog
    root = Tkinter.Tk()
    root.withdraw()
    file_path = tkFileDialog.askopenfile()

but the dialog box is ALMOST opening and then ArcMap crashes.

  • Use PyQT instead – Jason Scheirer Apr 18 '15 at 0:48
  • I am not familiar with PyQT. Is that another programming language? – Toronto23 Apr 18 '15 at 2:45
  • It's another graphics library – Jason Scheirer Apr 18 '15 at 2:46
4

Since you're using a python addin, you could use the pythonaddins.OpenDialog method. Slightly modified example based on the documentation:

This add-in button uses OpenDialog() to select a set of layer files and adds each layer to the selected data frame.

import arcpy
import pythonaddins

class AddLayers(object):
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        layer_files = pythonaddins.OpenDialog('Select Layers', True, r'C:\GISData', 'Add')
        mxd = arcpy.mapping.MapDocument('current')
        df = mxd.activeDataFrame
        for dataset in datasets:
            layer = arcpy.mapping.Layer(dataset)
            arcpy.mapping.AddLayer(df, layer)

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.