arcpy – How to Create a Tkinter Dialog Box in arcpy

arcpypython-addintkinter

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.

Best Answer

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)
Related Question