Creating Simple

GIS Applications

with Python

Spring 2016

Sam Giebner

Kris Johnson

Workshop Materials

Outline

Introduction - Tools & Tkinter

    Exercise #1

 

Classes - Code organization

    Exercise #2

 

Hooks - Capture user interaction

    Exercise #3

 

Modules - Connecting the dots

    Exercise #4

What is your experience with Python?

  1. A python is a type of snake
  2. Dabbled
  3. Aspiring apprentice
  4. Regular user
  5. Snake Charmer

Why create a Python GUI?

A Story about Steve...

Before

After

Some additional reasons:

  • cross-platform (               )
  • online/offline
  • GIS tasks with esri or OS
  • simple distribution
  • tap into the power of Python!

Tkinter

What is Tkinter?

  • most commonly used GUI toolkit for Python 
  • object-oriented layer on top of Tcl/Tk
    • Tcl: Tool Command Languege
    • Tk: standard Tcl GUI toolkit 

basically, it's a collection of Python modules

Tkinter App: 3 Main Parts

1. Tk instance

2. Widgets

3. Tkinter event loop

Tk Instance


    from Tkinter import *

    root = Tk()

18 Core Widget Classes

  • Button

  • Canvas

  • Checkbutton

  • Entry

  • Frame

  • Label

  • Listbox

  • Menu

  • Menubutton

  • Message

  • Radiobutton

  • Scale

  • Scrollbar

  • Text

  • Toplevel

  • LabelFrame

  • PanedWindow

  • Spinbox
    # Create widget instance     
    new_button = Button(tk_instance)

    # Style widget
    new_button['text'] = 'Click Here'
    new_button['command'] = do_something()

    # Place widget
    new_button.grid(row=0, column=2)

Widget Workflow

Initiate Tkinter event loop


    root.mainloop()

Classes

What are they?

  • A fundamental building block of Python
  • Logical grouping of data and functions
  • Blueprint for createing objects
  • Include bound functions called methods

    class NewClass():

        def __init__.py(self):
            pass

        def method_one(self):
            print("I'm a method!")

__init__ & self

self: refers to the object instance


    __init__(self):
        pass

__init__: the method that initilzes the instance

Application Organization

Interface (view) class

  • generates interface
  • handles user interaction
  • connects to processing modules

Processing (controller) class

  • processes data provided by user (via view)
  • returns product to interface

Hooks

Hooks or Callbacks

Capture, tie in user interaction

def callback():
    print "clicked!"

Button(text="click me", command=callback)
def callback(number):
    print "button", number

Button(text="one",   command=lambda: callback(1)).pack()
Button(text="two",   command=lambda: callback(2)).pack()
Button(text="three", command=lambda: callback(3)).pack()

Using a single callback for multiple widgets; lamda allows for passing args to callback

Modules

What is a Module?

  • A Python object that you can bind and reference
    • Contain:
      • Functions
      • Classes
      • Variables
      • Runnable code

Package

__init__.py

Module.py

Subpackage

__init__.py

Module.py

What is a Package?

  • A collection of related modules

Imports

Global import:


    from data_processing.csv2shp_esri import CSV2SHP_ESRI

    import arcpy

Local import:

  • __init__.py
  • adding directories to the python path sys.append

Distributing Your App

Considerations

  • Execution

  • Dependencies

Execution

Run the python (.py) script

 

Run batch (shell) script that calls python script

  • ​​Useful for sending system parameters to app (such as python version)

 

Use a packaging system like PyInstaller to create a single-file executable

  • Beware that these setups might fail to include system-wide installs like arcpy

 

Dependencies

Make sure all application dependencies are installed in the root directory of the primary python script

  • This may not be possible with libraries such as arcpy, and the end-user should be aware of application requirements such as this

 

Avoid hard-coding path names

 

Folder structure should remain intact

 

Packaging systems can help with some of these issues

 

Thank You

Sam Giebner

samg@northpointgis.com

Kris Johnson

krisj@northpointgis.com

Creating Simple GIS Applications with Python Spring 2016

By North Point Geographic Solutions

Creating Simple GIS Applications with Python Spring 2016

Using a stand alone graphic user interface (GUI) allows for the development of powerful apps that can utilize a wide variety of GIS software and can be easily shared cross-platform. This workshop will teach you the basics of creating stand alone GUI’s for your Python scripts using the Tkinter native Python package. In this workshop, you will learn the necessary Python concepts for creating Tkinter GUI’s, including classes and modules, as well as how to utilize the many widgets available in the Tkinter package. We will also create several simple GIS tools with the Tkinter GUI and discuss tool sharing best practices. You’ll leave with the skills and knowledge necessary for creating your own stand alone Python app!

  • 1,038