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.