TransWikia.com

Waiting for a python script tool to complete while using pythonaddin button

Geographic Information Systems Asked on July 3, 2021

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:pathtoaddintoolbar_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.

2 Answers

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 by Jason Scheirer on July 3, 2021

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

Answered by Chris on July 3, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP