Python script to enter name string

1716
14
Jump to solution
07-23-2013 05:26 AM
JacobDrvar
New Contributor II
I am attempting to write a Python script that selects a record in the attribute table, zooms to the selected feature, and prompts the user to enter their name. The name field already exists in the attribute table as a string type. How would I get the script to prompt the user to enter their name?Then, have the information they enter stored in the attribute table? I am going to have the users run the script from ArcToolbox.  I have tried the python addin to toolbar/button.  But, was unable to prompt the user to enter their name or zoom to selected feature without manually selecting the feature.

import arcpy  #Variables Parcels = arcpy.GetParameterAsText(0) arcpy.AddMessage(Parcels)  mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] lyr = arcpy.mapping.ListLayers(mxd, "Parcels", df)[0] arcpy.AddMessage(lyr.name)  #Logic try:  whereClause = "RENUM = "+Parcels+""  arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", whereClause)   df.extent = lyr.getSelectedExtent()  df.scale = df.scale*1.1  except:  print arcpy.GetMessages()
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JamesCrandall
MVP Frequent Contributor
James,

After much frustration, I was finally able to convert some of my tools to buttons.  Back, to the initial part of my search.  Can you provide an example string that would ask the user to provide their name?  I would like to have the value entered directly into a field in the attribute table.  Or at least point in the correct direction with which function to use.

Thanks again.


So, if your Add-In successfully launches/opens the correct BCPAO.tbx component...

In your "NameDate" BCPAO.tbx Toolbox, add a new paramter and set it as String.

At the top of this NameDate.py, set a variable to this parameter:

 inputName = arcpy.GetParameter(0) 


Now you can use this variable in an updateCursor on your set of selected features (that's a whole other thread! 🙂

View solution in original post

0 Kudos
14 Replies
JamesCrandall
MVP Frequent Contributor
I'd have the Python Add-In with a button.  Just configure the OnClick event of this add-in to launch your toolbox that has a string input parameter.


import pythonaddins
class ButtonClass_Button_1(object):
    """Implementation for MyAddin_addin.button_1 (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):        
       pythonaddins.GPToolDialog('C:\MyToolboxFoler\MyCustomToolbox.tbx', 'MyScriptTool') 



This MyScriptTool you reference would open and present the user the ability to enter in the string parameter.  Clicking the Ok button would run the SelectFeatures and zoom to selected code that you'd place in it.
0 Kudos
JacobDrvar
New Contributor II
James,

I will try your suggestion and see what happens.

Thanks.
0 Kudos
JamesCrandall
MVP Frequent Contributor
James,

Logically, this seems like a long way around.  Why create a button to call a toolbox?  Why not just use the script in the toolbox?  I will give it a shot and see if it works though.  I am not try to come off as ungrateful or rude.  I am new to python and scripting and I am just trying to gain some more knowledge and understanding. 

Thanks.


Not rude at all. 

The reason is because the toolbox needs to be launched in some manner -- the user can just access the Toolbox in the integrated ArcCatalog tree and I think that is just fine of an option.  But the Add-In allows for some user interaction FIRST with the map itself (which is what I thought you needed) --- the problem is that there are no real good options for GUI development with the add-in.  So, I just "use" the Toolbox as my User Interface and launch it from the Python Add-in as I suggested.

For example, what if you want the user to draw a polygon (an add-in tool) on the map that performs a SelectByLocation, then immediately after it finishes it selection, you want a GUI to popup that can present the user to input some information and save that info to the features that were selected by the tool?

I don't see how you can do this with just a Toolbox.  I mean, sure, you could just force the user to make some selections with the default Select Features tool, then just have them manually double-click your toolbox tool.  But that is what seems like a long way around!  The idea for creating an Add-In is to aggregate your tools into a single, unique toolbar that you can distribute to your users.

Hope that makes sense!
0 Kudos
JacobDrvar
New Contributor II
James,

I understand your logic.  Thanks for taking the time to explain your advice. 

I am struggling to get the Python addin.py to work.  I create a new folder, run the addin_assistant.exe, create a toolbar, create a new button, Save the ArcGIS Python Add-in Wizard, edit the python_addin.py, save the python_addin.py, run the makeaddin.py, run the python.esriaddin, install add-in, installation succeedes, customize add-in manager, customize the new add-in, and turn on new toolbar.  The button does not work.  So I am guessing that my addin script is not working.  So, then I go back, edit the script, delete my esriaddin and reun the final few steps. Sometimes the button displays the image selected and other times not.  The code is listed below.  All I did was change the toolbox location & name and included the script name.  Any ideas why the button is not working?

import arcpy
import pythonaddins

class UpdatesDates_Name(object):
 """Implementation for UpdatesDates_Name.button (Button)"""
 def __init__(self):
  self.enabled = True
  self.checked = False
 def onClick(self):
  pythonaddins.GPToolDialog('E:\User\Python\BCPAO.tbx', 'NameDate.py')
0 Kudos
JamesCrandall
MVP Frequent Contributor
Welcome to the club.  I struggled to get the add-in to work as intended (most of my dev work was with ArcObjects/COM components with installers).  To test to see if the actual button click event is getting fired, just try to present a message box

import arcpy
import pythonaddins

class UpdatesDates_Name(object):
 """Implementation for UpdatesDates_Name.button (Button)"""
 def __init__(self):
  self.enabled = True
  self.checked = False
 def onClick(self):
                  msg = "The click event worked!"
                  pythonaddins.MessageBox(msg, 'Just a test', 0)



Edit/Addition: if the msg pops up okay, then I'd guess there is an issue with the path to your toolbox or something.  Maybe use the UNC path if it is on a network location (that's what we do anyway, because not everyone I distribute will have the same letter'ed mapped drives).
0 Kudos
JacobDrvar
New Contributor II
I double checked the toolbox path and it is fine.  I included the latest suggestion, to add the message.  Nothing.  I have the toolbar name, but no picture or button name.  But, if I only edit the script to incude the message and accept all other defaults, it works.  ESRI must be having issues with the addin_assistant.exe.
0 Kudos
JamesCrandall
MVP Frequent Contributor
I double checked the toolbox path and it is fine.  I included the latest suggestion, to add the message.  Nothing.  I have the toolbar name, but no picture or button name.


Then there is something wrong with the building/registering the add-in steps.  Soooo frustrating!
0 Kudos
JacobDrvar
New Contributor II
I was able to add the button picture this time and return the print message.  But, the pythonaddins.GPToolDialog still does not run.  I believe the .exe fails when you attempt to change ID (Variable Names) for the toolbar/button.
0 Kudos
JacobDrvar
New Contributor II
I got the button to work.  But, in setting up the add-in, it seems to only work about 1 out of every 10 attempts.  Any one else run into the same or similiar problems with creating python add-ins?
0 Kudos