6

I'm developing an interface with arcgis and python.

When I have only python, I can make my script interactive using:

S = float(raw_input( 'Please enter the value: '))

I can open and run my script at the python window of the ArcGIS, and the script run, but when I try become this interactive, asking the value, ArcGIS gives me an error:

Runtime error 
Traceback (most recent call last):
  File "<string>", line 769, in <module>
EOFError: EOF when reading a line
10

Python window is not an equivalent of the Python shell; hence, you won't be able to use the raw_input there. To implement the interactivity with the user, you may choose any of these alternatives:

  • build custom script tools with input parameters (via arcpy.GetParameterAsText());
  • build Python add-ins (which have text boxes to fill in);
  • use 3rd party Python GUI toolkits such as Tkinter, wxPython, and PyQt.
  • 2
    Probably worth noting that some Python GUI toolkits (tkinter) don't play nicely with ArcGIS. There are ways of using those, but I've personally had mixed results and sometimes end up completely crashing ArcGIS. – Evil Genius Apr 21 '16 at 16:29
  • I saw what you answer but I still not understand.. In your 2nd reference, I saw this code: import arcpy # Get the spatial reference from the tool dialog. spatial_ref = arcpy.GetParameter(0) # Display the Spatial Reference properties arcpy.AddMessage("Name is: {0}".format(spatial_ref.name)) arcpy.AddMessage("Type is: {0}".format(spatial_ref.type)) arcpy.AddMessage("Factory code is: {0}".format(spatial_ref.factoryCode)) So in python window of arcgis it will never be interactive? – S.Rai Apr 21 '16 at 16:32
  • 1
    I suggest that you review a Help section on creating script tools, this will give you an idea what kind of interactivity is available. resources.arcgis.com/en/help/main/10.2/index.html#/… – Alex Tereshenkov Apr 21 '16 at 16:55
  • 1
    @EvilGenius, I agree. I've had quite a success with PyQt and ArcGIS Pro Python 3.4 though. – Alex Tereshenkov Apr 27 '16 at 6:15
  • @Alex, can you tell what you mean by build Python add-ins (which have text boxes to fill in); – AMurray May 4 '16 at 17:09
1

I have a workaround I've been using in production for 2 years. Not pretty, but works reliably. In your py script you call a batch file which gets a string of user input and writes it to a temp text file. Then python reads that file.

The batch file (getInput.bat):

@ echo off

REM  Takes a single line of user input and writes it to an ascii file.  More details below.
REM    (See also getChoice.bat which takes a single keystroke and doesn't require "ENTER, control-Z and ENTER"

echo.
echo ==== GETINPUT.BAT =============================================
echo.
echo   USER INPUT: respond with a line of text.
echo     If you make a mistake, simply retype it -- this tool
echo     saves just one line: the last _non-blank_ line entered.
echo. 
echo   To finish, press  ENTER,  control-Z  and ENTER
echo.
echo.
echo *** %1
echo.


REM A blank input (eg: ^Z ENTER) leaves the output file unchanged, so  
REM    need to set a default string 
SET USERINPUT=NULL

REM Closing the DOS window without input also leaves the output file unchanged, so need also
REM    to overwrite the old file -- otherwise previous input is used.  Note that the python 
REM    code below explicitly checks for "NULL" 
echo NULL > B:\users\%username%\userinput.txt 

REM Get userinput and send it to an environment variable.
FOR /F "tokens=*" %%A IN ('TYPE CON') DO SET USERINPUT=%%A

REM  Send the value of the environment variable to a file, for python to read.
echo %USERINPUT% > B:\users\%username%\userinput.txt 

The python code:

import getpass
import subprocess
username = getpass.getuser()
userInputFile = "B:/users/" + username + "/userinput.txt"
msgarg = "Type row and column of the Cell.  Numbers only, separated by a space."
# (use doublequotes so CMD sees it as one argument)

# run the batch file -- which prompts for input and writes it to a temp file
subprocess.call([r"B:\software\utilities\getInput.bat",msgarg])

# read the user's input from that file
with open(userInputFile) as inputFile:
    for line in inputFile:
        if line:
            userInput = line.rstrip()
            break   # stop at the first line read 
if line == "NULL":   
   # do something to respond to NULL input, e.g. ...
   print = "Aborting process based on user input (" + userInput + ")"
  • I just changed this line: subprocess.call([r"F:\Otimizacao\correto\ficheiro.gms",msgarg]) but arcgis gives me error in this line: Runtime error Traceback (most recent call last): File "<string>", line 10, in <module> File "C:\Python27\ArcGIS10.2\Lib\subprocess.py", line 493, in call return Popen(*popenargs, **kwargs).wait() File "C:\Python27\ArcGIS10.2\Lib\subprocess.py", line 679, in init errread, errwrite) File "C:\Python27\ArcGIS10.2\Lib\subprocess.py", line 896, in _execute_child startupinfo) WindowsError: [Error 193] %1 is not a valid Win32 application Do you understand why? @AMurray – S.Rai Apr 28 '16 at 11:07
  • What kind of file is ficheiro.gms ? It is supposed to be getInput.bat which is a file containing the first block of code above. – AMurray Apr 30 '16 at 2:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.