3

I have created a pythonaddin toolbar, with a button that executes a python script tool. This tool is in an ArcToolbox which has been stored in the Install folder of the addin. It executes and runs perfectly. However, the rest of the script in the pythonaddin button class does not wait for the tool to finish. I searched on the ESRI help forums, and there was a suggestion that I use a while loop to wait for the result of the tool. However, I do not believe that a custom python script tool returns a result object like arcpy gp tools.

My toolbar (toolbar_addin.py) code

parameterList = []
class button1(object):
 def__init__(self):
 self.enabled=True
 self.checked=False
 def onClick(self):
 ### Get path to external toolbox
 toolbox = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'Toolbox.tbx')
 ### execute external script tool
 pythonaddins.GPToolDialog(toolbox, 'tool')
 ### wait for result from gp tool
 while len(parameterList) == 0:
 time.sleep(0.2)
 #do something with items from parameterList

My tool (tool.py) code

import toolbar_addin
input1 = arcpy.GetParameterAsText(0)
input2 = arcpy.GetParameterAsText(1)
#do something to create output1 and output2...
toolbar_addin.parameterList = [output1, output2]

Now I get the following error:

Traceback (most recent call last):
File "C:\path\to\addin\toolbar_addin.py", line 229, in onClick
 time.sleep(0.2)
TypeError: GPToolDialog() takes at most 1 argument (2 given)

Any suggestions on how I can force my code to wait on the gp tool?

EDIT: I've modified the fuctioning of my button and script tool to resemble this post. The tool passes parameters to a global list variable "parameterList" in the toolbar.

asked May 12, 2014 at 21:05
0

2 Answers 2

3

The behavior of the GPToolDialog function is only documented as follows: Opens a geoprocessing tool dialog box.

The function asks the application to bring up a dialog window, but does not return any status/result object to you. It does not offer any way of directly determining when the tool finishes running or if it gets cancelled.

answered May 12, 2014 at 21:12
6
  • +1 for the answer, but I am really trying to determine if the tool can be run synchronously. Commented May 13, 2014 at 16:30
  • I implemented that function. It does run synchronously up until 10.2.1 or so when I had to change it to run async to enable interactive selection in the dialogs for feature set inputs. The function was only designed to pop up a tool dialog, not give any feedback. How/when it runs is an implementation detail. Commented May 13, 2014 at 16:44
  • Thanks again, I'm trying to see if the tool, not the dialog, can be run synchronously. Commented May 13, 2014 at 16:47
  • You cannot force your code in an add-in event to wait on the tool as it is popped up from GPToolDialog. You cannot communicate with the result object like you can when you call the tool directly from Python. You will need to look into other control flow constructs to get your desired behavior. Commented May 13, 2014 at 16:50
  • Understood, please see my edit. Commented May 13, 2014 at 16:52
2

I have been having this exact same problem (using ArcPy 10.4.1) and came up with a workaround.

What I did is create a new tool (I'm calling it "new_tool") to serve as a wrapper for the actual tool I want to run. new_tool has the same parameters as the tool you actually want to call, and it calls the tool you want to run and then does whatever useful stuff with the output you want to.

For your code you could look something like this:

parameterList = []
class button1(object):
 def__init__(self):
 self.enabled=True
 self.checked=False
 def onClick(self):
 ### Get path to external toolbox
 toolbox = os.path.join(os.path.dirname(os.path.abspath(__file__)), 
'Toolbox.tbx')
 ### execute external script tool
 pythonaddins.GPToolDialog(toolbox, 'new_tool')

and new_tool would look something like:

#Get however many parameters you need
param1 = arcpy.GetParameter(0)
param2 = arcpy.GetParameter(1)
#call the tool you actually want to use
tbx = arcpy.ImportToolbox(os.path.join(os.path.dirname(__file__), "Toolbox.tbx")) 
tbx.tool(param1,param2)
#Do whatever you wanted to do with the output here
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
answered Dec 18, 2017 at 18:36

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.