Select to view content in your preferred language

pythonaddins

7267
7
05-12-2015 03:11 PM
DonStazic
Emerging Contributor

Hi,

I had posted this in Developers but it's been sitting there a couple of days with no response so I thought I'd try this space instead.

...

The answer is most probably glaring me in the face but I can't see it.

I needed to activate a Python tool dialog through a Addin button and GPToolDialog looks like the answer.

The following is the script, can't be much simpler.

import arcpy
import pythonaddins
class ButtonClass1(object):
    """Implementation for Addin_Test_addin.button (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        pythonaddins.GPToolDialog(r'C:\Users\stazicd\AppData\Roaming\ESRI\Desktop10.1\ArcToolbox\My Toolboxes\PVP Zoom.tbx','PVPZoom')

The button runs and after some time eventually zooms out to the full extent of the map document.

Don't be distracted by that zoom and think that my tool isn't working correctly. If I only have a  simple pythonaddins.MessageBox the same thing happens. It looks like the script isn't finding pythonaddins.

The button does work if I only have code in onClick that doesn't have any pythonaddins so the setup of the button is OK.

To check I ran the above GPToolDialog command in ArcMap using the Python command line window, things worked as they should.

Looks like I'm missing something in a set up somewhere. Any suggestions folks.

Cheers,

Don

(Curtis Price​ formatted the code 5/15/2015)

0 Kudos
7 Replies
DuncanHornby
MVP Notable Contributor

Don,

I'm using 10.3 and tested your code. I created a pythonaddin tool bar with a single button on it. The model in my toolbox ran without a problem and like you it also ran from the python command line.

What I did notice by chance (as I had the python window open) is it threw up an error message before the actual model tool opened:

TypeError: GPToolDialog() takes at most 1 argument (2 given)

I then tested out of curiosity a model with and without parameters, both opened with the same TypeError message appearing in the python command line window. I also tried one parameter like C:\temp\toolbox.tbx\mymodel and it threw an error saying it was missing tool_name argument!

What is not clear from your explanation is if the tool you are trying to open in the toolbox is a model tool or a script tool? May be this is an issue?

DonStazic
Emerging Contributor

Thanks Duncan for looking into that at depth, I'm looking into those.

You say that "The model in my toolbox ran without a problem", do you mean toolbar?

The script tool when I run it from a toolbox under My Toolboxes works OK. The error message is interesting as GPToolDialog has two parameters.  ArcGIS Help 10.1

0 Kudos
DuncanHornby
MVP Notable Contributor

I created a pythonaddin which contained a single toolbar with a single button. The onClick event is calling a model in a toolbox. Just like your code:

pythonaddins.GPToolDialog(r'C:\Users\stazicd\AppData\Roaming\ESRI\Desktop10.1\ArcToolbox\My Toolboxes\PVP Zoom.tbx','PVPZoom')

Your toolbox is PVP Zoom.tbx and you are calling the tool PVPZoom. But from this alone I cannot tell if you are calling a script tool or a model tool? In my test I was calling a model and it worked but was throwing that odd TypeError message in the python console for some reason...

But in your reply above you now say it was a script tool (some python exposed as a tool). I'm wondering if it is to do with that. Try making a simple model and running your pythonaddin to call that. If that works (as it did in my case) we can at least say your pythonaddin code is correct and it's something to do with the script code that you are calling?

RebeccaStrauch__GISP
MVP Emeritus

I ran into the

TypeError: GPToolDialog() takes at most 1 argument (2 given

error a while back which resulted in bug NIM089253. I was able to find a work around and get it to work.that worked and I posted it Tip: Python Addin - getting custom tools/toolbox to work - GPToolDialog

I hope this helps.  If not, I may have a few more tips and suggestions.

curtvprice
MVP Esteemed Contributor

Please excuse the meta...

I had posted this in Developers but it's been sitting there a couple of days with no response so I thought I'd try this space instead.

In the future, Don Stazic please consider using the Share button on the right side of the discussion page. Or just add an "@mention" like this Developers  makes the post visible to more groups - but keeps all the comments in one page. Much better that way!

Share a post with a group

DonStazic
Emerging Contributor

Thanks everyone for their input, I've learnt some stuff and tried both Rebecca's and Duncan's ideas. I haven't been able to look at this over the past week or so but I'm back onto it now.

I haven't though been able to find a solution to my problem. The onClick event is being entered, that I'm sure of as I put in code that didn't use any pythonaddins and that worked. Three lines of code that just zoomed to selected features.

I've tried in onClick event just having the following line

      pythonaddins.MessageBox("Hello","Check")

Similar to calling pythonaddins.GPToolDialog() the program just keeps running, no message appears, so the problem appears to be has pythonaddins been imported even though that statement is there.

Developers

0 Kudos
DonStazic
Emerging Contributor

Just to add. If I have the following the zoom works fine and in this case the code doesn't wait as if its executing something. The message still doesn't print. If I didn't have the 3 lines of the selection code and just the message the addin just sits there looking like it's executing.

import arcpy
import pythonaddins
import os.path

class ZoomCase(object):
    """Implementation for PNF_Zoom_Case_addin.button (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        mxd = arcpy.mapping.MapDocument("CURRENT")
        df = arcpy.mapping.ListDataFrames(mxd)[0]
        df.zoomToSelectedFeatures()       
       
        pythonaddins.MessageBox("Hello","Check")

0 Kudos