Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have bunch of buttons to perform separate tasks within a Python Add-In. Buttons are greyed out and required to be enabled after initial tools run. In the following example, tool1 is turned off after execution using its ID and button1 is enabled. Classes are arranged alphabetically at the time of Add-In construction. This configuration of classes works sometimes and fails at others with message: "NameError: global name 'button1' is not defined". This is confusing as to why it runs sometimes and fails at others. Can anyone please help with explanation as to why is it the case and how to better control the state of buttons without error?

class Calc(object):
    """Implementation for test_addin.button1 (Button)"""
    def __init__(self):
        self.enabled = False
        self.checked = False
    def onClick(self):
        """do something"""

class process(object):
    """Implementation for test_addin.tool1 (Tool)"""
    def __init__(self):
        self.enabled = True
        self.cursor = 3

    def onMouseDownMap(self, x, y, button, shift):
        """do something"""

        tool1.enabled = False
        button1.enabled = True
share|improve this question

2 Answers 2

In most cases this should just work, it may be that it's just not instantiating the tool until it's first clicked. You could set the attributes on the class instead:

def onMouseDownMap(self, x, y, button, shift):
    """do something"""

    process.enabled = False
    Calc.enabled = True

or trap the sets in NameErrors:

def onMouseDownMap(self, x, y, button, shift):
    """do something"""

    try:
        tool1.enabled = False
        button1.enabled = True
    except NameError: # Some variable isn't instantiated yet
        pass
share|improve this answer
    
Use of the class name or ID to enable buttons isn't working. I tried trap the sets in NameErrors, it repeatedly gives "isn't instantiated" error. –  Ibe Jul 2 '13 at 1:35
up vote -1 down vote accepted

The state of buttons can be controlled with the answer provided here.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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