Sam Giebner
Jesse Adams
Slides
http://slides.com/northpointgis/simple_python_applications_ws
Introduction - Tools & Tkinter
Exercise #1
Classes - Code organization
Exercise #2
Hooks - Capture user interaction
Exercise #3
Modules - Connecting the dots
Exercise #4
Request
Coworker
Client
basically, it's a collection of Python modules
1. Tk instance
2. Widgets
3. Tkinter event loop
from Tkinter import *
root = Tk()
Button
Canvas
Checkbutton
Entry
Frame
Label
Listbox
Menu
Menubutton
Message
Radiobutton
Scale
Scrollbar
Text
Toplevel
LabelFrame
PanedWindow
# 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
root.mainloop()
class NewClass():
def __init__(self):
pass
def method_one(self):
print("I'm a method!")
self: refers to the object instance
__init__(self):
pass
__init__: the method that initilzes the instance
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
Package
__init__.py
Module.py
Subpackage
__init__.py
Module.py
Global import:
from data_processing.csv2shp_esri import CSV2SHP_ESRI
import arcpy
Local import:
Run the python (.py) script
Run batch (shell) script that calls python script
Use a packaging system like PyInstaller to create a single-file executable
Make sure all application dependencies are installed in the root directory of the primary python script
Avoid hard-coding path names
Folder structure should remain intact
Packaging systems can help with some of these issues
Sam Giebner
samg@northpointgis.com
Jesse Adams
jesse@northpointgis.com