1

I am trying to create a simple tool to copy XY from where i click the mouse button. What i have done already is:

  1. create add-in files using Python Add-In Wizard
  2. create toolbar and tool inside it

in *.py file i got:

import arcpy
import pythonaddins
import win32clipboard as clipboard
class p_tool(object):
def __init__(self):
 self.enabled = True
 self.shape = "NONE" 
def onMouseDownMap(self, x, y, button, shift):
 button = 1
 shift = 2
 clipboard.OpenClipboard()
 clipboard.EmptyClipboard()
 xy = str(x)+' '+str(y)
 clipboard.SetClipboardData(xy, clipboard.CF_TEXT)
 clipboard.closeClipboard()
 message = xy
 pythonaddins.MessageBox(message, "My Coordinates", 0) 

About win32clipboard - i tested in in python windon in ArcMap and I am able to import it and openclipboard() but emptyclipboard() does not work and gives me that error:

File "", line 1, in error: (1418, 'EmptyClipboard', 'Thread does not have a clipboard open.')

asked Oct 22, 2014 at 9:10

1 Answer 1

1

The following code uses the approach of calling a subprocess and piping the text to the clipboard.

A couple of things to note:

  1. I use the onMouseUpMap event, this returns the XY coordinates in map units.
  2. I placed a comma between the numbers so there is no space between them.
  3. Wrapped the code up in a try-except to capture any failures.
import arcpy
import pythonaddins
import subprocess
class p_tool(object):
 """Implementation for pythonaddintest_addin.tool (Tool)"""
 def __init__(self):
 self.enabled = True
 self.shape = "NONE"
def onMouseUpMap(self, x, y, button, shift):
 try:
 xy = str(x)+','+str(y)
 command = "echo " + xy + " | clip"
 i=subprocess.check_call(command, shell=True)
 message = xy
 pythonaddins.MessageBox(message, 'My Coordinates', 0)
 except Exception as e:
 pythonaddins.MessageBox(str(e),"Error",0)
answered Oct 24, 2014 at 15:48

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.