1

I made an Add-in for ArcMap that shows important news for our company. It is actually a toolbar with combobox and refresh button. It loads news from a text file located in our server and as every user has an access to it, this toolbar loads lines from this file. Finally it looks like this:

Image

Here is a part of code

class gc_cmb(object):
"""Implementation for GC_NewsToolbar_addin.combobox (ComboBox)"""
def __init__(self):
    file_txt =  u'\\\SRV-03\\Projects\\UTILS\\AddIns\\\\news.txt'
    list_news = []
    with open(file_txt, 'r') as myfile:
        list_news=myfile.read().split('\n')

    self.value = list_news[0]
    self.items = list_news
    self.editable = True
    self.enabled = True
    self.dropdownWidth = 'WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW'
    self.width = 'WWWWWWWWWWWWWWWWW'

User can see the latest new in a combobox's window here self.value = list_news[0]. Other news are in a dropdown list. The refresh button updates this list. But there one thing I would like to add:

Auto-update - It would be much better if news are refreshng automtically after text file is edited. Looks like writing some kind of timer but it probably can interrupt geoprocessing tools if they are launch.

Any thoughts?

3

I've not had much call in developing Python AddIns but what you were asking is something I would have approached in VB .net using an extension with an event firing on something specific.

If you look at the help file you can create a Python add-in application extension. This can hook into various events, an obvious one is on openDocument() or possibly contentsChanged(); you could get the combobox to refresh on that.

Below is a basic outline of me adding things to the list, in your case you would use your code to read from your text file. I hook into 3 events to improve the regular updating of the combobox.

import arcpy
import pythonaddins


class myComboBoxClass(object):
    """Implementation for PythonAddIns_addin.combobox (ComboBox)"""
    def __init__(self):
        self.items = ["item1", "item2"]
        self.editable = True
        self.enabled = True
        self.dropdownWidth = 'WWWWWW'
        self.width = 'WWWWWW'
    def onSelChange(self, selection):
        pass
    def onEditChange(self, text):
        pass
    def onFocus(self, focused):
        # Add text to list on click of control as suggested by Kadir
        if focused:
            self.items.append("Click me!")
    def onEnter(self):
        pass
    def refresh(self):
        pass

class myExtensionClass(object):
    """Implementation for PythonAddIns_addin.extension2 (Extension)"""

    # Note to access the combobox I use the name after the full stop in PythonAddIns_addin.combobox, so combobox
    def __init__(self):
        # For performance considerations, please remove all unused methods in this class.
        self.enabled = True
    def openDocument(self):
        combobox.items.append("FUNKY")
    def contentsChanged(self):
        combobox.items.append("WOMBAT")
    def itemAdded(self, new_item):
        combobox.items.append("ROBOT")
  • exactly what I thought, I tested it with def onStartEditing(self) and def onSaveEdits(self), it was working well. – Pavel Pereverzev Aug 3 '18 at 5:44
2

I doubt you can use timer in addin seamlessly. If possible, I want to learn, too. However, you can use onFocus() function of combobox. This is not a perfect solution, but it might be helpful.

class gc_cmb(object):

    def __init__(self):
        self.editable = True
        self.enabled = True
        self.dropdownWidth = 'WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW'
        self.width = 'WWWWWWWWWWWWWWWWW'
        self.getNews() ####

    def onFocus(self, focused):
        if focused:
            self.getNews() ####

    def getNews(self): ####
        file_txt =  u'\\\SRV-03\\Projects\\UTILS\\AddIns\\\\news.txt'
        list_news = []
        with open(file_txt, 'r') as myfile:
            list_news = myfile.read().split('\n')

        self.value = list_news[0]
        self.items = list_news

In that way, when combobox gets focus by any user, content of combobox changes.

  • onFocus() means moving mouse cursor to the combobox window? – Pavel Pereverzev Aug 2 '18 at 11:53
  • Not just moving mouse cursor to the combobox, you have to click the combobox to focus. I tried the script on a file in my desktop folder and it worked. But of course, I ignore if text file was editted. Every time you click the combobox to show items, items are updated. – Kadir Şahbaz Aug 2 '18 at 12:13
  • also tested it but it's also the same action as my refresh button. Unfortunately it seems to be impossible to keep combobox updating automatically – Pavel Pereverzev Aug 2 '18 at 12:16
  • Yes. The script doesn't refresh combobox automatically, it refreshes when just clicking the combobox. That will save you from extra refresh button. – Kadir Şahbaz Aug 2 '18 at 12:20

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.