Sunday 31 March 2019

python - TKinter to provide variables


I've been looking for a way to enter a unique field name into an attribute table for up to 40 feature classes. I need the field names to be meaningful, not just 'field1', 'field2' etc. For example, if the feature class represents wetlands I would like a field called 'Wetland'.


I thought the easiest way of doing this would be to use TKinter. I can print the feature class name and then dynamically enter a field name with the TKinter data entry msgbox. Then keep looping through the feature classes until they all have a field with a unique name.


I've found some examples but am unsure about how to use data entry text as a variable in Python. If anyone out there had an example script or can make suggestions on this script I'd appreciate it:


import arcpy
from arcpy import env
from Tkinter import *

# Environment Workspace
env.workspace = r'G:\TEST\Python_Test\Test.gdb'


def receive():
text = E1.get()
print (text)
top = Tk()
L1 = Label(top, text="User Name")
L1.pack( side = LEFT)
E1 = Entry(top, bd =5)
E1.pack(side = RIGHT)
b = Button(top, text="OK", width=10, command=receive)

b.pack()
top.mainloop()

Answer



I managed to figure this out... My code now iterates through feature classes, requests a field name, calculates fields, deletes unwanted fields. And then joins the left over fields to each other with a spatial join. I was stuck on adding the interactive tkinter dialog box in the first loop, and the delete field text at the end of the first loop. I still have some to do on this, but thought I'd post the answers to my original question. Thought I'd put my code up for others:


import Tkinter
import tkSimpleDialog
import arcpy
from arcpy import env

env.workspace = r'G:\TEST\Python_Test\Test.gdb\env'


fcList = arcpy.ListFeatureClasses()

for fc in fcList:
print fc
root = Tkinter.Tk()
var = tkSimpleDialog.askstring("Field Name", "Enter Field Name")
print var

arcpy.AddField_management(fc, var, "TEXT", "", "", "50", "", "NULLABLE", "REQUIRED", "")

print "Adding field name " + var + " to " + fc
arcpy.CalculateField_management(fc, var, "\"Y\"", "PYTHON", "")
print "Calculating field " + var + " to " + fc

keep = ['OBJECTID', 'Shape', 'Tower', var, 'Shape_Area', 'Shape_Length']

discard = []
for field in [f.name for f in arcpy.ListFields(fc)if f.type <> 'OBJECTID']:
if field not in keep:
discard.append(field)

arcpy.DeleteField_management(fc, discard)
print "Deleted all fields except those specified"

# Input variables for spatial join
Buffer1 = r'G:\Test\Python_Test\Test.gdb\Buffer1'
Buffer2 = r'G:\Test\Python_Test\Test.gdb\Buffer2'

sjList = arcpy.ListFeatureClasses()

# For loop for spatial join process


for sj in sjList:

# Spatial join between buffered points and feature
arcpy.SpatialJoin_analysis(Buffer1, sj, Buffer2, "JOIN_ONE_TO_ONE", "KEEP_ALL")
print "Completed spatial join " + sj

# Delete Buffer1 containing no joined features
arcpy.Delete_management(Buffer1, "FeatureClass")
print "deleted Buffer1 " + sj


# Copies Buffer2 containing joined features and outputs as Buffer1
arcpy.Copy_management(Buffer2, Buffer1, "")
print "Copied Buffer2 to Buffer1 " + sj

# Deletes Buffer2 to allow script to create Buffer2 in next iteration
arcpy.Delete_management(Buffer2, "FeatureClass")
print "Deleted Buffer2 " + sj

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...