Get user input text in arcpy

23943
27
Jump to solution
06-07-2012 11:12 AM
WilliamIde
New Contributor II
I want to ask the user what name to give to a report.  I could use Tkinter to do this but it seem there should be something in arcpy to do it.   Just pop up a little dialog ask for some text and return that text as a string.  Is there something like that?

Thanks.
Tags (2)
0 Kudos
27 Replies
WilliamMcLay
New Contributor III

I am attempting to ask the user for a "proposal number" and simply zoom in on that proposal. I can call the Pop-Up Box and enter the value for the "proposal". But that value is not being passed into "SelectByAttribute_management" (see code below). Python says "AttributeError 'module' object has no attribute". Any help would be appreciated!

Thank You.

#Importing Modules
import arcpy

#Call PopUpBox
try:
    arcpy.ImportToolbox(r"F:\Data\Arcadwin\Toolboxes\PopUp.tbx")
    result=arcpy.PopupInputBox_popup("A message from popup box")
    in_val=result.getOutput(0)
    print in_val.upper()

except arcpy.ExecuteError:
    print arcpy.GetMessages(2)

except Exception as ex:
    print ex.args[0]

#Define WhereClause
WhereClause = '"MCPC_Num"' + "='" + str(in_val) + "'"

#Find the layer
mxd=arcpy.mapping.MapDocument("CURRENT")
df=arcpy.mapping.ListDataFrames(mxd,"Layers")[0]

#Select by Attributes
arcpy.SelectByAttribute_management("Year 2015 Proposals", "NEW_SELECTION", WhereClause)

#Zoom to Selection
df.ZoomToSelectedFeatures()

#Refresh
arcpy.RefreshActiveView()

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

William, you may want to fork this to a new question since the main is answered....might get more response.  You can mention this one as related.  Also, check out Posting Code blocks in the new GeoNet

NobbirAhmed
Esri Regular Contributor

@William McLay,

Download the ZoomToFeature Python add-in from here:

http://www.arcgis.com/home/item.html?id=e753f3287f4a46c98c60e3e5c6a21463

Unzip the download - you'll see a folder named Install - there is a word document inside that folder. The doc describes how to use the add-in.

NobbirAhmed
Esri Regular Contributor

Hi @steve kenneth, which version of ArcGIS you are using? 10.1 or later? If so then you can use arcpy.da.SearchCursor to step through data row by row as shown below. If you are on 9.3 or earlier then use arcpy.SearchCursor instead (see documentation for sample code). Consult this topic more details:

Accessing data using cursors—Help | ArcGIS for Desktop

This topic contains code examples for arcpy.da.SearchCursor:

SearchCursor—Help | ArcGIS for Desktop

There is an argument named where_clause - set it in a similar way you'd in a SQL expression. The above topic has examples.

stevekenneth
New Contributor II

Thanks. I'm using arcgis10.2 . I don't know if it's because of me or my link but the second link you share return an error. I'm Going to wait to take a look to examples but i think you your reply is more helpfull Thann expected. Hope it's  possible to tag people on topics because i have other  threads for what i would have your help

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

http://desktop.arcgis.com/en/desktop/latest/analyze/arcpy-data-access/searchcursor-class.htm

Nobbir's second link, I believe.  I know the system has had error with a few links lately.....may nee to cut and past above.

10.2 version ArcGIS Help (10.2, 10.2.1, and 10.2.2)

or http://resources.arcgis.com/en/help/main/10.2/index.html#//018w00000011000000​   if cut and past needed.

0 Kudos
TI
by
Occasional Contributor II

You can use a script tool as others have mentioned.  But the piece of the puzzle that was missing for me until now, was how to pass the user input from the script tool back to the Add-In.  I have just found a method of doing this at https://gis.stackexchange.com/questions/75691/accessing-the-output-of-a-python-script-tool-dialog-ar... .

In brief, you can call your AddIn python module and any of its classes/variables functions from your script tool.  I have tested this and it works fine for me.  In brief, in your script tool you can do it simply like this:

import arcpy, MyCustomTools_addin

MyCustomTools_addin.findLayersButton.findLayers(arcpy.GetParameterAsText(0))

In this case my AddIn module is 'MyCustomTools_addin' and it has a button with the id 'findLayersButton' (note that this is the instantiated object, or id, not just the class name). This class has a function called 'findLayers()'.

I simply pass the only parameter from the tool to this AddIn class function (which then adds the layers to the map in a new layer group called "Search Results").

This is still not ideal, as the script tool dialogue stays on screen when it completes (which is good in most cases).  I would prefer the script tool to disappear as soon as it has run (the user can configure this to happen, but then that would happen for all script tools, not just for this one).

I also like to display/log more useful information in my script tools.  So in my tool I actually do it more like what is below.  For my AddIn, the output of the function is simply a list of layers that match the string entered by the user.  So the tool then displays the output in it's log window with AddMessage().

import arcpy, MyCustomTools_addin

results = MyCustomTools_addin.findLayersButton.findLayers(arcpy.GetParameterAsText(0))
arcpy.AddMessage("\n\n\nRESULTS:\n\n{}\n\n".format("\n".join(results)))
0 Kudos
NobbirAhmed
Esri Regular Contributor

Wow – that’s great. ☺

0 Kudos