Wednesday, 24 August 2016

python addin - Creating Tkinter dialog box in arcpy?


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.



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)

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...