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.

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Apr 17, 2015 at 23:59
3
  • Use PyQT instead Commented Apr 18, 2015 at 0:48
  • I am not familiar with PyQT. Is that another programming language? Commented Apr 18, 2015 at 2:45
  • It's another graphics library Commented Apr 18, 2015 at 2:46

1 Answer 1

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)
answered Apr 18, 2015 at 3:36
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.