Many ArcGIS analysts and developers working with Python tools in ArcGIS Desktop or ArcGIS Pro would like to be able to build custom Python script tools extending the built-in framework of the GP tools dialog. I have answered a question Recommendations about graphic interface for ArcPy/Python script providing useful links on how to build an own GUI inside ArcGIS desktop application. I have also blogged about this topic in another post – Building custom UI tools for ArcGIS with Python.
Other users have also shown some progress. I have seen that Erik Pimpler has a couple of videos (Building GUI Applications for ArcGIS Desktop with Python – Part 1, Building GUI Applications for ArcGIS Desktop with Python – Part 2) that showcase how one can build custom GUI inside ArcMap using
wxPython – a Python framework for GUI development. I have also seen Andrew Chapkowski integrating
Tkinter into a Python add-in, but this is basically it. However, I do see that many users would like to be able to build own GUI inside ArcGIS using Python and/or using a designer or form builder to facilitate the process of the layout preparation. There is even an idea Form Builder for Python Tools on the ArcGIS Ideas website with quite a few votes.
When building a GUI app with Python, my personal choice is
PyQt. It’s extremely mature and robust framework for GUI development which is a pleasure to work with. I have built some GUI utilities for text editing (think some of the Linux
grep functionality in a Windows app) as well as SQL editor to work with file geodatabases. I have been incredibly productive building applications really quickly. There are so many useful tutorials on PyQt, so I have never really have stuck at any problem. You can build a helpful utility application in one evening.
Resources on PyQt development:
QGIS developers are familiar with this framework because they build QGIS plugins using PyQt, however if you are an ArcGIS developer, this is pretty new to you unless you have used PyQt for other projects. So, let’s see how you can set up an PyQt application with ArcGIS Pro.
- Install ArcGIS Pro (further just Pro). You will get a default
conda environment called
arcgispro-py3 which you can copy first into a new environment so you could restore it should something bad happen with the default one which we will be playing with.
- With the help of Python Package Manager in Pro, you need to install
pyqt 5.6.0 into the default environment. This will quite take some time (5-10 mins).
- Now you are ready to write a PyQt application that can be called from a Python script tool.
- Run Pro, create and then save a new empty project.
- Create a new custom script tool in a toolbox and point it to a Python file with the following content.
Now you can run the script tool just like you would any other custom script tool and you should get this tiny app window open:
Note: if you are getting an error message trying to run the tool:
This application failed to start because it could not find or load the Qt platform plugin “windows”.
then you need to add a system environment variable
QT_QPA_PLATFORM_PLUGIN_PATH with the path to the
plugins folder used by PyQt5. If you have installed ArcGIS Pro into a default location for all users, you would need to provide this value:
C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Library\plugins\platforms. Restart ArcGIS Pro to let it grab the environment variable on the next start.
If you did get this window – congratulations, you have just accessed
arcpy from a Python program that was run as a PyQt5 application. You have accessed the currently open project in Pro and accessed its property. So, what does this imply?
This means you can write code within a PyQt5 application that will be accessing your current map and its layers as well as basically everything you can reach with
arcpy. Now it is up to you to choose what GUI to build as Qt framework provides excellent set of widgets to choose from.
Note: you may have noticed that after you have run the script tool and the PyQt app window was opened, Pro thought that the script tool is still running, so the script tool will complete only upon closing the application. This behavior can be changed if you run your PyQt application in own thread, so as your application starts, the script tool will complete its execution. This can be done using the
Create a new Python Python module to use as the source code for the script tool:
This is what you would see.
Note: since the application runs in its own thread, it won’t be able to access the current project: it will run as if you were running it as a standalone script. You can of course still use
arcpy and other packages you have in your Pro
To give you a feeling of how powerful PyQt is and what kind of amazing tools you can build to let Pro users to work with, I’ve created a sample app that can showcase some of the PyQt power. In this application, you can choose an input point feature class and it will generate something called alpha shape (or concave hull). I’ve adopted some of the code you can find in the blog posts Drawing Boundaries In Python by HumanGeo and The fading shape of alpha by Sean Gillies.
In the application, you can choose what edges of the triangulation you would like to keep based on the distance between the edge vertices by using an interactive slider. To visualize the produced data, embedded version of
Matplotlib plot is drawn. When you are happy with the result, you can add the produced data to ArcGIS Pro current map as layers. I will post the source code of this application in another post later on.
Below is the screen recording of running the Pro application and starting a custom script tool that in turn starts a PyQt application window.