I tried to specify a feature class from a GDB dataset through Python Tkinter tkFileDialog.askopenfilename. Below is the script. It worked for me in the past when I was dealing with .txt or .las files. However, this time it is not giving me the GDB feature class, which has no file extension here. Please advise.
import arcpy
from arcpy import env
import Tkinter,tkFileDialog
from Tkinter import *
import tkMessageBox
import ttk
from tkFileDialog import askopenfilename
from tkFileDialog import askopenfile
env.workspace = "C:\\temp\\miniPilot\\miniPilot.gdb"
def browseFor_inFC():
inFC_name = tkFileDialog.askopenfilename(parent=root, initialdir=r'C:\temp\miniPilot\miniPilot.gdb\data', \
title='Choose a feature class')
inFC.set(inFC_name)
def cancel():
root.destroy()
def remove():
fields = arcpy.ListFields(inFC)
for field in fields:
print field.name
if field.name <> "OBJECTID" and field.name <> "SHAPE" and field.name <> "SHAPE_Length":
arcpy.DeleteField_management(inFC, field.name)
root=Tk()
root.title("XXXXXX")
# The first frame named 'mainframe'
mainframe = Frame(root, relief='flat', borderwidth=6)
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)
# set up Tkinter variables, which are global variables, for the first gui -- the root window
inFC = StringVar()
# widgets on the mainframe
Label(mainframe, text="Specify the feature class: ").grid(column=1, row=1, sticky=E)
inFC_entry = Entry(mainframe, width=36, textvariable=inFC)
inFC_entry.grid(column=2, row=1, sticky=(W, E))
inFC_browser_Button=Button(mainframe, text="...", height=1, width=3, command=browseFor_inFC).grid(column=3, row=1, sticky=W)
# two button widgets
refract_Button=Button(mainframe, text="OK", command=remove).grid(column=0, row=2, sticky=E)
cancel_Button=Button(mainframe, text="CANCEL", command=cancel).grid(column=4, row=3, sticky=W)
for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)
root.mainloop() # create an event loop
Best Answer
Here's the update: I used tkFileDialog.askdirectory() to get the gdb name to set the workspace. Then I used arcpy.da.Walk() from the data access module to load the feature classes into an array. My next step would be, put a ttk combobox on the GUI and load feature classes into the drop-down of the combobox so users get to specify the feature class to do whatever further manipulations. A portion of the script is listed below. Definitely, I am going to try pythonaddins.OpenDialog, too. Thanks Jason for pointing to a new direction.