<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Python Add-In, Combo Box and Zoom to Selected Unstable in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/python-add-in-combo-box-and-zoom-to-selected/m-p/642222#M50063</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I am trying to create a python add-in toolbar with a combo box.&amp;nbsp; The combo box lists all of the unique [SITE_NUM] values in my SITE feature class.&amp;nbsp; The user should be able to select a SITE_NUM from the combo box and have the active map view zoom to the selected site.&amp;nbsp; The problem is that the toolbar doesn't behave consistently.&amp;nbsp; It will work fine in one map session and then won't work at all when the same document is opened a minute later with no saved changes.&amp;nbsp; I'm new to python and add-ins so I'm not sure where I'm going wrong.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;My code is as follows:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy import pythonaddins&amp;nbsp;&amp;nbsp; global mxd mxd = arcpy.mapping.MapDocument('current') global df df = arcpy.mapping.ListDataFrames(mxd, "Layers") [0]&amp;nbsp;&amp;nbsp; class ComboBoxClass1(object): &amp;nbsp;&amp;nbsp;&amp;nbsp; """Implementation for Sites_addin.combobox (ComboBox)""" &amp;nbsp;&amp;nbsp;&amp;nbsp; def __init__(self): &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.editable = True &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.enabled = True &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.dropdownWidth = 'WWWWWWWWWW' &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.width = 'WWWWWWWWWW'&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; def onFocus(self, focused): &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; layer = r"C:\...\SITE" &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.items = [] &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; values = [row[0] for row in arcpy.da.SearchCursor(layer, ["SITE_NUM"])] &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for uniqueVal in sorted(set(values)): &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.items.append(uniqueVal)&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; def onSelChange(self, selection): &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; layer = r"C:\...\SITE" &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management(layer, "Selection") &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management("Selection", "NEW_SELECTION", "SITE_NUM = '" + selection + "'") &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.RefreshActiveView() &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; df.zoomToSelectedFeatures() &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.RefreshActiveView()&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If anyone has any suggestions as to how to improve my code and forgo the creation of an entirely new "Selection" layer, but keep the combo box populated with all of the SITE_NUM values after a selection is made, I would greatly appreciate it.&amp;nbsp; I'm truly stumped.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 24 Mar 2014 19:51:47 GMT</pubDate>
    <dc:creator>Anonymous User</dc:creator>
    <dc:date>2014-03-24T19:51:47Z</dc:date>
    <item>
      <title>Python Add-In, Combo Box and Zoom to Selected Unstable</title>
      <link>https://community.esri.com/t5/python-questions/python-add-in-combo-box-and-zoom-to-selected/m-p/642222#M50063</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I am trying to create a python add-in toolbar with a combo box.&amp;nbsp; The combo box lists all of the unique [SITE_NUM] values in my SITE feature class.&amp;nbsp; The user should be able to select a SITE_NUM from the combo box and have the active map view zoom to the selected site.&amp;nbsp; The problem is that the toolbar doesn't behave consistently.&amp;nbsp; It will work fine in one map session and then won't work at all when the same document is opened a minute later with no saved changes.&amp;nbsp; I'm new to python and add-ins so I'm not sure where I'm going wrong.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;My code is as follows:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy import pythonaddins&amp;nbsp;&amp;nbsp; global mxd mxd = arcpy.mapping.MapDocument('current') global df df = arcpy.mapping.ListDataFrames(mxd, "Layers") [0]&amp;nbsp;&amp;nbsp; class ComboBoxClass1(object): &amp;nbsp;&amp;nbsp;&amp;nbsp; """Implementation for Sites_addin.combobox (ComboBox)""" &amp;nbsp;&amp;nbsp;&amp;nbsp; def __init__(self): &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.editable = True &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.enabled = True &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.dropdownWidth = 'WWWWWWWWWW' &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.width = 'WWWWWWWWWW'&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; def onFocus(self, focused): &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; layer = r"C:\...\SITE" &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.items = [] &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; values = [row[0] for row in arcpy.da.SearchCursor(layer, ["SITE_NUM"])] &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for uniqueVal in sorted(set(values)): &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.items.append(uniqueVal)&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; def onSelChange(self, selection): &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; layer = r"C:\...\SITE" &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management(layer, "Selection") &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management("Selection", "NEW_SELECTION", "SITE_NUM = '" + selection + "'") &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.RefreshActiveView() &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; df.zoomToSelectedFeatures() &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.RefreshActiveView()&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If anyone has any suggestions as to how to improve my code and forgo the creation of an entirely new "Selection" layer, but keep the combo box populated with all of the SITE_NUM values after a selection is made, I would greatly appreciate it.&amp;nbsp; I'm truly stumped.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 24 Mar 2014 19:51:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-add-in-combo-box-and-zoom-to-selected/m-p/642222#M50063</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2014-03-24T19:51:47Z</dc:date>
    </item>
    <item>
      <title>Re: Python Add-In, Combo Box and Zoom to Selected Unstable</title>
      <link>https://community.esri.com/t5/python-questions/python-add-in-combo-box-and-zoom-to-selected/m-p/642223#M50064</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi, I'm fairly new to this as well, and have been working on a very similar add-in recently.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I think what might be happening is that you're getting the reference to the mxd and dataframe globally, outside the classes, so that code is only being executed once, so when you load a new mxd, your script doesn't have a reference to it. Try moving this code into the onFocus() function, that way whenever the user clicks the combo box, it will update the reference to the mxd and the dataframe, as well as refreshing the list of SITE_NUMs.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If the layer you are selecting from already exists in the mxd, you can do something like:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;layer = arcpy.mapping.ListLayers(mxd, "Name of Layer", df)[0]&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;to create a reference to the first layer in the mxd called "Name of Layer", and then pass the layer variable to the arcpy.SelectLayerByAttribute_management() function to avoid creating a new selection layer.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Note also that the zoomToSelectedFeatures() function will zoom to the extent of all selected features on ALL layers, which may not be what you want; you can also do:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
df.extent = layer.getSelectedExtent()
arcpy.RefreshActiveView()
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Hope that helps!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Dan&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 03:17:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-add-in-combo-box-and-zoom-to-selected/m-p/642223#M50064</guid>
      <dc:creator>DanEvans</dc:creator>
      <dc:date>2021-12-12T03:17:37Z</dc:date>
    </item>
    <item>
      <title>Re: Python Add-In, Combo Box and Zoom to Selected Unstable</title>
      <link>https://community.esri.com/t5/python-questions/python-add-in-combo-box-and-zoom-to-selected/m-p/642224#M50065</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks so much Dan!&amp;nbsp; I've implemented your suggestions into my code and it's working much better!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I have another question; the combo box is populated with all SITE_NUM's when the user selects their first site.&amp;nbsp; But the other sites that were available before disappear when the user tries to select a second site.&amp;nbsp; That's why I originally created the "Selection" layer, above.&amp;nbsp; One (partial) solution is to clear my selection in the onFocus function, but it's not ideal.&amp;nbsp; Ideally I would like to maintain the selection so the user can generate a site report, etc. based on their selection, but still have the complete list of sites available to them for further selection later.&amp;nbsp; Any thoughts?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;My new and improved code... &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
class ComboBoxClass1(object):
&amp;nbsp;&amp;nbsp;&amp;nbsp; """Implementation for Sites_addin.combobox (ComboBox)"""
&amp;nbsp;&amp;nbsp;&amp;nbsp; def __init__(self):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.editable = True
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.enabled = True
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.dropdownWidth = 'WWWWWWWWWW'
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.width = 'WWWWWWWWWW'

&amp;nbsp;&amp;nbsp;&amp;nbsp; def onSelChange(self, selection):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; layer = arcpy.mapping.ListLayers(mxd, "SITE", df)[0]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(layer, "NEW_SELECTION", "SITE_NUM = '" + selection + "'")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; df.extent = layer.getSelectedExtent()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.RefreshActiveView()

&amp;nbsp;&amp;nbsp;&amp;nbsp; def onFocus(self, focused):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; global mxd
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd = arcpy.mapping.MapDocument('current')
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; global df
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; df = arcpy.mapping.ListDataFrames(mxd, "Layers") [0]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; layer = arcpy.mapping.ListLayers(mxd, "SITE", df)[0]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(layer, "CLEAR_SELECTION")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.items = []
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; values = [row[0] for row in arcpy.da.SearchCursor(layer, ["SITE_NUM"])]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for uniqueVal in sorted(set(values)):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.items.append(uniqueVal)
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks again.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 03:17:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-add-in-combo-box-and-zoom-to-selected/m-p/642224#M50065</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-12-12T03:17:40Z</dc:date>
    </item>
    <item>
      <title>Re: Python Add-In, Combo Box and Zoom to Selected Unstable</title>
      <link>https://community.esri.com/t5/python-questions/python-add-in-combo-box-and-zoom-to-selected/m-p/642225#M50066</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi Emily,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I think what you need to do is populate the combo box from the underlying SITES feature class, rather than the layer. You can get the path for the data source of the layer like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;layer_path = layer.dataSource&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;then pass the layer_path to the code that finds the unique values, like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;values = [row[0] for row in arcpy.da.SearchCursor(layer_path, ["SITE_NUM"])]&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;That should allow you to remove the clear selection from onFocus()&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I think that should work, let me know if it doesn't!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Dan&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 25 Mar 2014 15:25:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-add-in-combo-box-and-zoom-to-selected/m-p/642225#M50066</guid>
      <dc:creator>DanEvans</dc:creator>
      <dc:date>2014-03-25T15:25:56Z</dc:date>
    </item>
    <item>
      <title>Re: Python Add-In, Combo Box and Zoom to Selected Unstable</title>
      <link>https://community.esri.com/t5/python-questions/python-add-in-combo-box-and-zoom-to-selected/m-p/642226#M50067</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Worked like a charm!&amp;nbsp; Thanks so much!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 25 Mar 2014 15:40:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-add-in-combo-box-and-zoom-to-selected/m-p/642226#M50067</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2014-03-25T15:40:28Z</dc:date>
    </item>
  </channel>
</rss>

