I am trying to create a simple tool to copy XY from where i click the mouse button. What i have done already is:
- create add-in files using Python Add-In Wizard
- 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.')
1 Answer 1
The following code uses the approach of calling a subprocess and piping the text to the clipboard.
A couple of things to note:
- I use the onMouseUpMap event, this returns the XY coordinates in map units.
- I placed a comma between the numbers so there is no space between them.
- 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)
Explore related questions
See similar questions with these tags.