Select to view content in your preferred language

pythonaddins.GPToolDialog

1533
3
07-22-2013 07:44 AM
AultonSmith1
New Contributor
I am having problems with retrieving and using user input in phthonaddins. The only tool available that I know of is the GPToolDialog function (if there is an alternative, please let me know). I have tried using it several ways, but none work satisfactorily. The 1st way is a two button approach, in which the function is used in one button, and the rest of the code is run in a second button. This works, but is cumbersome. I have tried a one button approach several ways. When I invoke the GPToolDialog in my primary add-in code, I see the Parameters Dialog for the 2nd tool, but no further code in the primary .pyt will run after the parameters are set. I can add code in the 2nd tool, and it will run, but, strangely, only if I click the screen while the "Executing GetParameters (the 2nd tool)" window is running. How do I retrieve and use the parameters set by the user?
Thank You,
Aulton Smith
Tags (2)
0 Kudos
3 Replies
AdamWehmann
Deactivated User
Here are some things I've done:

Scenario 1: You need to use outputs immediately, i.e.
class Button(object):
 def onClick(self):
  result = PromptUser("Enter information")
  # do something with the result


When this use case can't be solved using the pythonaddins module, one solution is to launch a custom Tkinter dialog in a subprocess and then pipe in the results.  This works best if you don't have a lot of data to pass, although you can read and write from a temporary file to pass more data.

cf. http://forums.arcgis.com/threads/65186-Python-Add-In-User-Input

Scenario 2: You don't need to use the outputs immediately, i.e.
result = None

class Button1(object):
 def onClick(self):
  global result
  result = PromptUser("Enter information")

class Button2(object):
 def onClick(self):
  global result
  if result is not None
   # do something with the result
   result = None


One way to solve this is just to apply Scenario 1 and then output the results to a global variable.

Another way to solve this is to launch a Python tool and read and write your inputs and outputs from temporary files.  This makes the most sense when you need the user to execute a tool, but want to control the tool inputs and outputs.

To do this, within Button1.onClick(), you would write any inputs you need for the tool (i.e. the results of previous user interaction in your add-in) to a temporary file and then launch the tool from a toolbox you've included in your add-in's \Install directory.  This toolbox is not otherwise exposed to the user.  Next, in the __init__ for the tool, read the temporary file to retrieve the inputs.  It's guaranteed to be there since you just wrote it prior to launching the tool.  In the execution() of the tool, after using the temporary file inputs and the tool parameter inputs to do your processing, write your outputs to a secondary temporary file.  Finally, in Button2.onClick(), check whether this output temporary file exists.  When it does, read it in, delete it, and then do whatever needs to be done with its data.

You can use pickling to easily pass variables back and forth.

---

GPToolDialog launches the tool asynchronously.  It's just designed as a shortcut to pop open a tool dialog, which is why I think you're having problems.
0 Kudos
TimBarnes
Frequent Contributor
Is it possible to pass arguments to a GP tool using  pythonaddins.GPToolDialog?

I.e.
Arg1 = 'The path of a shapefile'
Arg2 = 'Some other value obtained before invoking the tool'

start = pythonaddins.GPToolDialog("G:\\FIS_Tools\\FIS_Tools.tbx", 'GIStoGPX', 'Arg1', 'Arg2')

I just want to use the pythonaddins.GetSelectedTOCLayerOrDataFrame() to populate the input shapefile by default.
0 Kudos
AdamWehmann
Deactivated User
Not as far as I know, but that should definitely be an update to GPToolDialog() in the future.
0 Kudos