Select to view content in your preferred language

Python Add-In User Input

6464
6
08-21-2012 10:07 AM
JustinShepard
Occasional Contributor II
I was so excited to see that there is now a way to create add-ins using Python. However, now that I've got my add-in created and I'm trying to edit the script I noticed that user input still isn't fully supported. There are a few options for getting input, http://resources.arcgis.com/en/help/main/10.1/index.html#/The_pythonaddins_module/014p00000021000000..., but I need to prompt my users to enter different values (such as text or numbers). As expected input and raw_input still don't work.

Is there a way to get text and numbers from the user? If not will ESRI be adding this functionality to the pythonaddins module soon. Unless I can get full user input support this option for creating add-ins will rarely, if ever, be used by me.
Tags (2)
0 Kudos
6 Replies
BryanTruong
New Contributor
You can use Tkinter GUI package to create user interface and get user inputs. You can create a class with an arcpy and user inputs properties as properties and do all geoprocessing based on user inputs.
Tkinter references can be found here http://docs.python.org/library/tkinter.html
0 Kudos
JustinShepard
Occasional Contributor II
But will Tkinter be packaged with the add-in when I distribute it? I distribute my add-ins to other people and I don't want them to have to install additional libraries/interfaces such as Tkinter (and in some cases they aren't allowed to).
0 Kudos
JustinShepard
Occasional Contributor II
Is there a way to use arcpy parameter like you would in a python toolbox?
0 Kudos
MathewCoyle
Frequent Contributor
But will Tkinter be packaged with the add-in when I distribute it? I distribute my add-ins to other people and I don't want them to have to install additional libraries/interfaces such as Tkinter (and in some cases they aren't allowed to).


Tkinter is a standard library, anyone with Python installed will have access to it.

Is there a way to use arcpy parameter like you would in a python toolbox?


Input parameters should be able to be handled the exact same way as a python toolbox.
0 Kudos
JohnDye
Regular Contributor
Tkinter will not work. It will crash everytime. Tkinter for some reason, it wasn't specified why, does not play well with the ArcGIS framework. This was mentioned in the Python Addin Seminar http://training.esri.com/gateway/index.cfm?fa=catalog.webCourseDetail&CourseID=2485. There is a work around using wxPython GUIs but it looks like it might still be a little unstable. http://betablogs.esri.com/beta/arcgis/2012/05/03/custom-wxpython-guis-an-approach-for-arcgis-10-1/
0 Kudos
AdamWehmann
New Contributor II
Tkinter doesn't play nice with Python add-ins, but one way to work around it is to launch dialogs in a subprocess and then read the output from the shell.

For example, if you needed an Open File dialog with filtering, you might put the following script named as "file_open.py" in your add-in's install directory:

# file_open.py

import Tkinter
import tkFileDialog
import sys

# Get command line arguments
args = sys.argv
args_len = len(args)

# Create parent window and hide it from view
root = Tkinter.Tk()
root.withdraw()

# Generate dialog options
options = {}
options['parent'] = root
if args_len == 2:
    options['title'] = args[1]
elif args_len == 3:
    options['title'] = args[1]
    options['initialdir'] = args[2]
options['multiple'] = False
options['filetypes'] = [('Special file','*.special'),('All files','*')]

# Show dialog
file_path = tkFileDialog.askopenfilename(**options)

# Destroy parent window
root.destroy()

# Print result
print file_path


Then, use the following inside your add-in script:

import arcpy
import os
from subprocess import Popen, PIPE

def open_file_dialog(dialog_title, default_dir=None):
    file_open = "file_open.py"
    file_path = os.path.join(
        os.path.dirname(os.path.abspath(__file__)), file_open)
    file_arg = [file_path, dialog_title]
    if default_dir is not None:
        file_arg.append(default_dir)
    proc = Popen(file_arg, shell=True, stdout=PIPE, bufsize=1)
    stdoutdata, stderrdata = proc.communicate()
    proc.terminate()
    return stdoutdata.strip()

file_path = open_file_dialog("Open File")
if arcpy.Exists(file_path):
    pass


If you need text or numeric input, you can use other Tkinter window functions similarly to build the appropriate dialog.  In general though, you might think whether you can rework your add-in to function without the input or whether the functionality you are trying to implement would better exist in a toolbox.
0 Kudos