How to control state of buttons in Python Add-In?

3201
10
Jump to solution
07-02-2013 07:00 AM
IbraheemKhan1
New Contributor III
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
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JasonScheirer
Occasional Contributor III
It's an optimization in the Add-In framework which makes less sense in Python than it does in C# Add-Ins: it only creates the objects when it needs them.

My response in this thread should give a workaround -- basically, it pre-initializes your button variables.

View solution in original post

0 Kudos
10 Replies
JasonScheirer
Occasional Contributor III
Please paste a full traceback(s) as it appears in the Python window.
0 Kudos
JasonScheirer
Occasional Contributor III
Please paste EVERYTHING that appears in the Python window, not just the error type.

Attached is a simple add-in that does what yours does, and it seems to work fine. I suggest you re-make your addin's .py file from the wizard and try again.
0 Kudos
IbraheemKhan1
New Contributor III
I am only getting following error:
Python: NameError: global name 'button1' is not defined

Is it because of the fact that there are many different classes between "tool1" and "button1", in actual -- Just guessing.
0 Kudos
JasonScheirer
Occasional Contributor III
Check the python prompt, type YourAddIn_addin. (where the module name is the same as the filename without the .py extension) and see if there is auto-complete. Is the button1 variable there? See if you can create it yourself, if you can make a YourAddinIn_addin.Calc() or if it throws an exception.
0 Kudos
IbraheemKhan1
New Contributor III
If I correctly understood what you have said,
I typed "YourAddinIn_addin" in Python interactive window and it returned:

<module 'YourAddinIn_addin' from 'C:\Users\Ibe\AppData\Local\ESRI\Desktop10.1\AssemblyCache\{301A358E-60E7-0717-8A9E-86368EAA3D1F}\YourAddinIn_addin.py'>

but "YourAddinIn_addin.Calc()" throws back:

Runtime error
Traceback (most recent call last):
  File "<string>", line 1, in <module>
AttributeError: 'module' object has no attribute 'Calc'
0 Kudos
DavidAllen
Occasional Contributor
I am only getting following error:
Python: NameError: global name 'button1' is not defined


Here's my shot-in-the-dark answer.
Do you by any chance have the button1 on a menu within a toolbar? If so, the button doesn't initialize until the menu is clicked. Here's what I'm experiencing with a button/tool issue of my own. The tool is on the toolbar, a menu is on the toolbar, and the button is in the menu.

Start ArcMap and open the map document. Click the menu name, then use the tool - the enabled property of the button works fine.
Start ArcMap and open the map document. Click the tool and I get the error message "global name 'printButton' is not defined.

Since the menu isn't controlled in the Python script, I don't see a fix.

David Allen
0 Kudos
JasonScheirer
Occasional Contributor III
So it appears as though the Calc class does not exist. What does dir(YourAddinIn_addin) tell you? Can you run the .py file on its own from python.exe without syntax errors?
0 Kudos
IbraheemKhan1
New Contributor III
So it appears as though the Calc class does not exist. What does dir(YourAddinIn_addin) tell you? Can you run the .py file on its own from python.exe without syntax errors?


dir(YourAddinIn_addin) returns various ArcGIS builtin functions including classes, functions, and modules that I am using in my AddIn. There were few global name definition errors during running *.py file from Install folder using python.exe

It is strange that solution proposed above by David works. So we have to initialize buttons first before clicking on tools, any idea why is it the case?
0 Kudos
JasonScheirer
Occasional Contributor III
It's an optimization in the Add-In framework which makes less sense in Python than it does in C# Add-Ins: it only creates the objects when it needs them.

My response in this thread should give a workaround -- basically, it pre-initializes your button variables.
0 Kudos