-1

I am having some issues. In ArcGIS, I created a custom toolbar (with the python addin wizard) to do the following: 1. Button to connect to feature class, 2-5. A series of comboboxes to view, edit data in a field in the feature class. I want each of the combo box classes to directly connect to the field. And be able to write new data from the boxes into the fields.

Example: Combobox 1 to connect to field 1. So that when I deploy this a user can edit data without doing this the traditional way. Its a way to have inexperienced users edit data. I am new to python and willing to pay for the time required to help me as I am out of ideas. And ESRI doesn't have a solution either.

import arcpy
import pythonaddins
from arcpy import env


class ButtonClass1(object):
    """Implementation for QuickTableEdit_addin.button (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        print("Button1 Clicked")
        env.workspace = "C:/data"
        arcpy.CreateTable_management("C:/data", "test_table.dbf","template.dbf")
        mxd = arcpy.mapping.MapDocument("CURRENT")
        df = arcpy.mapping.ListDataFrames(mxd)[0]
        dbf_Table = arcpy.mapping.TableView(r"C:\data\test_table.dbf")
        arcpy.mapping.AddTableView(df, dbf_Table)
        arcpy.RefreshTOC()
        pass

class ComboBoxClass2(object):
    """Implementation for QuickTableEdit_addin.combobox_1 (ComboBox)"""
    def __init__(self):
        self.items = [1,2]
        self.editable = True
        self.enabled = True
        self.dropdownWidth = 'WWWWWW'
        self.width = 'WWWWWW'
    def onSelChange(self, selection):
        global cb1
        cb1 = self.value
        pass
    def onEditChange(self, text):
        pass
    def onFocus(self, focused):
        pass
    def onEnter(self):
        pass
    def refresh(self):
        pass
  • I made a similar toolbar where I had a combobox to select a layer from the TOC, a tool to only select a single feature in the combobox selection, and two comboboxes linked to two specified fields in the feature class. Is this similar to what you are trying to do? – Barbarossa Feb 14 '14 at 21:50
  • Yes, at least the second part is. Basically if I have a layer called "work order" and a field called "Type". I want the combobox 'type' to show the data in the field. Consequently, if a user types in a new value that gets added or can be editing a new feature. – Bob C Feb 14 '14 at 21:54
3

Yeah, ESRI documentation is poor regarding how each function works. It took me a while to figure most of it out. Just trial and error, unfortunately.

The onSelChange function grabs the selection from self.items. onEditChange creates a text variable with whatever is typed into the combobox. So, everytime you type something in, it changes. So if you type in "NEW", the text variable will be "N", "NE", and then "NEW", as it is typed. The onFocus function does something whenever the combobox is selected, or has focus. In your case you would probably need to read from the field and populate self.items list with whatever items are in the field. The onEnter function does something whenever the ENTER button is hit, your case would probably be an update cursor.

Let me know if this helps, or if you need more clarification.

EDIT: I posted some of my code below. Hope it helps.

class CallTypeComboBoxClass(object):
    """Implementation for Custom_Tools_addin.combobox_3 (ComboBox)"""
    def __init__(self):
        self.items = []
        self.editable = True
        self.enabled = False
        self.dropdownWidth = 'WWWWWWWWWWWWWW'
        self.width = 'WWWWWWW'
    def onSelChange(self, selection):
        global typ_selection
        typ_selection = selection
    def onEditChange(self, text):
        global new_call
        new_call = text
    def onFocus(self, focused):
        if focused:
            self.items = []
            # Add all items from 'CALL_TYPE' field to combo box
            with arcpy.da.SearchCursor(call_type_table, ("CALL_TYPE")) as cursor:
                for row in cursor:
                    self.items.append(row[0])
    def onEnter(self):
        # if text is not already in combobox and has length greater than 0
        if new_call not in self.items and len(new_call) > 0:
            cursor = arcpy.da.InsertCursor(call_type_table, ("CALL_TYPE"))
            cursor.insertRow((new_call,))
            del cursor
    def refresh(self):
        pass
  • How would I connect this to a specific shapefile and/or database? Would I put this string into the "cursor = arcpy.da.InsertCursor(table?, ("field?"))" thank you for your response. – Bob C Feb 17 '14 at 14:56
  • 1
    Yeah, that's the syntax for the cursor. – Barbarossa Feb 17 '14 at 15:56
  • This doesn't have any syntax for update cursor? How will I update the database based on this code? – Bob C Feb 18 '14 at 15:55
  • 1
    My example is a very specific example. You can find documentation for arcpy update cursors here – Barbarossa Feb 18 '14 at 16:24
  • 1
    Unfortunately, with the given specificity, I am unable to offer more of a solution. The link above offers a couple of examples, at the bottom of the page, of how to update a fc in a database. How familiar are you with python? – Barbarossa Feb 18 '14 at 22:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.