Connect script with a sub-menu entry within Python-Addin

2071
3
03-21-2013 02:51 AM
IbraheemKhan1
New Contributor III
I have created a menu entry using "pythonaddins" and it contains 2 sub-menus. Two separate scripts have to be linked to the sub-menus which should be invoked one-by-one when I click on sub-menu entry. First sub-menu should be active and second greyed out. Please suggest how to do it.
Tags (2)
0 Kudos
3 Replies
JohnDye
Occasional Contributor III
Khan,
It's not clear what you are trying to do but one thing is certain, you can't execute a function with a menu or submenu. Menus are just containers for buttons or sub-menus. Creating a Menu or Sub-Menu does not instantiate an Object which is neccesary for you to be able to do anything. With Python Addins, you have three choices to create an Object. A Button, Tool or Combobox. Any of these three will instantiate an Object which you can then leverage to execute your scripts.

It sounds like you want to execute an external script based on a button click. To do this, you need to create a Toolbar and a Button, or Two buttons if you have two scripts unless you want to do some other conditional formatting, but based on your limited description, I don't think you do.

Once you create the Toolbar and 2 buttons, all you have to do is create 2 variables which reference the absolute path to the scripts you want to execute. Then use the execfile function to execute them when you click on the button.

Here is an example:
import arcpy
import pythonaddins

class Button1(object):
    """Implementation for LYR.BaseMap_Sat (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        script1 = "C:\Temp\Module1.py"
        execfile(script)

class Button2(object):
    """Implementation for LYR.BaseMap_Sat (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        script2 = "C:\Temp\Module2.py"
        execfile(script2)

If you want one of the buttons to be greyed out, then you need to change the 'enabled' property for that button to 'False'. If you want it to be enabled/disabled based on the status of the other button, then you need to do some conditional statements.

I suggest you take a look at the Python Addin Guidebook for more information.

Happy Scripting!
John
0 Kudos
IbraheemKhan1
New Contributor III
Thanks for the reply.
Actually I figured it that way. Earlier, I assumed that there has to be a class which I have to link with menu entry but guide book pointed to the use of button for sub-menu to call respective script under that button class.


**Right now, I am looking at possible way to get a message box with strings to select -- interactive string selection from message dialogue. I think it is not possible with pythonaddins current module options but hoping there to be a workaround for this. Sorry for posting off the topic text.
0 Kudos
JohnDye
Occasional Contributor III
There's no way in Python Addins to do that with a Messagebox that I can currently think of. You could certainly use a Combobox, with the items populated with your strings and the 'selection' as the method to call whatever function you want.
0 Kudos