1

With Python Add-In Wizard I created a ButtonTool with which I:

  1. click in the map
  2. get the coordinates of the mouseclick-location in a dialog with textboxes

I want to receive the coordinates in a textbox to have the possibility to copy and paste the coordinate values (e.g. to use them in a document). I get a crash between TKinter and arcpy like I read in some posts before. Nevertheless I want to ask, if anybody has experience with this and has a suggestion.

Here is my code:

Import Tkinter
class ToolCoord(object):
    def __init__(self):
        self.enabled = True
        self.shape = "NONE" 
        self.cursor = 3
    def onMouseDownMap(self, x, y, button, shift):
           pt = arcpy.PointGeometry(arcpy.Point(x,y))
           main = Tkinter.Tk()
           lb = Tkinter.Label(main, text = "UTM-Koordinaten:")
           lb.pack()
           t1 = Tkinter.Text(main, width=20, height=0)
           t1.insert("end", str(x))
           t1.pack()
           t2 = Tkinter.Text(main, width=20, height=0)
           t2.insert("end", str(y))
           t2.pack()
           main.mainloop()
2
  • 1
    Write to a temp text file and open it with notepad or write directly to the clipboard.
    – user2856
    Dec 18, 2015 at 20:30
  • @Luke: Thank you for the hint. That was a smart and simple solution. I made it with the Notepad. Feb 17, 2016 at 13:26

2 Answers 2

2

Tkinter and Esri Python Addins do not play nice together. You can use Tkinter within scripting with Acrpy, but it will usually crash when implemented in an Addin. wxPython GUIs will work within an ESRI Addin. However these will require the end user of your Addin to have wxPython installed on their machine.

1

Based on the hint of Luke I use now a eventbutton with the following code:

class ToolClass71(object):
    """Implementation for Python_Addins_addin.tool (Tool)"""
    def __init__(self):
        self.enabled = True
        self.shape = "NONE" # Can set to "Line", "Circle" or "Rectangle" for interactive shape drawing and to activate the onLine/Polygon/Circle event sinks.
        self.cursor = 3
    def onMouseDownMap(self, x, y, button, shift):
        pt = arcpy.PointGeometry(arcpy.Point(x,y))
        # set environment
        mxd = arcpy.mapping.MapDocument('current')
        mxdpath = mxd.filePath
        df = arcpy.mapping.ListDataFrames(mxd)[0]
        outWorkspace = os.path.dirname(mxdpath)
        # creating a temp feature in the memory space
        tempitem = arcpy.CreateFeatureclass_management("in_memory","item","POINT","","DISABLED","DISABLED","Arch_Denkmal_DSch")
        # add and populatin fields with coordinates
        fields = ["x_coords","y_coords"]  
        i = 0  
        for x in fields:  
            arcpy.AddField_management(tempitem, x, "DOUBLE")
            i = i + 1
        arcpy.da.InsertCursor(tempitem, ["SHAPE@XY"]).insertRow([pt])
        out_file = os.path.join(outWorkspace,"Koordinaten.txt")
        value=["x_coords","y_coords"]
        in_table = tempitem
        # export to csv
        arcpy.ExportXYv_stats(in_table,value,"SPACE",out_file,"ADD_FIELD_NAMES")
        # change rows and colums
        with open(out_file) as f:
            lis = [x.split() for x in f]
        with open(out_file, 'wb') as f:
          # Überschreiben der alten Datei mit den neuen Reihen
            writer = csv.writer(f)
            writer.writerows(zip(*lis))
            # csv öffnen
        subprocess.call(['C:/Windows/System32/notepad.exe', out_file])
        # delete temp objects and refresh active view
        arcpy.Delete_management("in_memory")
        arcpy.Delete_management("Koordinaten")
        arcpy.Delete_management(out_file)
        arcpy.Delete_management(outWorkspace+"\Koordinaten.xml")
        arcpy.RefreshActiveView()

Your Answer

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

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