1

I wrote a simple script bind to a button

import arcpy
import pythonaddins

class ButtonClass3(object):
    """Implementation for python_add_in_addin.button (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        import arcpy
        mxd = arcpy.mapping.MapDocument("CURRENT")
        for lyr in arcpy.mapping.ListLayers(mxd):
            if lyr.supports("DATASOURCE"):
            arcpy.AddMessage("Layer: {0} Source: {1} ".format(lyr.name, lyr.dataSource))

But when I click the button, nothing happens. I wonder where could I see the message. I am using ArcMap 10.2.2.

1

The arcpy.AddMessage() function only writes log messages to the geoprocessing dialog (when running a script tool).

Creates a geoprocessing informative message (Severity=0) that can be accessed with any of the GetMessages functions.

I suppose you could call a custom script tool to run on the button click event that would open this dialog which you could write messages to. Alternatively, for logging you could write out messages to your own text file.

|improve this answer|||||
  • Where is that geoprocessing dialog and how to open it?? – ElChapo Jun 20 '18 at 13:31
0

As artwork21 said, the geoprocessing dialog would only be visible if you created a custom script tool from your script.

Below is a sample setup for a logger that a coworker gave me when I started learning python - and he said I'm free to share it:

    #logging example
    import logging, os, time, sys

    def SetUpLogging(logdirpath):
        """Sets up the script logger (2 handlers: a file and the console)
        Keyword arguments:
        logdirpath -- path to logfile that will be written to 
        """
        #if output path doesn't exist, make it
        if not os.path.exists(logdirpath):
            os.makedirs(logdirpath)
        #nicely format the datetime
        nowDateTime = time.strftime("%Y%m%d%H%M%S")
        logTime = str(nowDateTime)
        scriptName = (os.path.splitext(os.path.basename(sys.argv[0])))[0]
        logName = "{0}_{1}{2}".format(scriptName,logTime,".log")
        logfilepath = os.path.join(logdirpath, logName)
        logger = logging.getLogger(scriptName)
        #handler for logging to a file
        hdlr = logging.FileHandler(logfilepath)
        formatter = logging.Formatter('%(asctime)s %(levelname)s % (message)s')
        hdlr.setFormatter(formatter)
        #handler for logging to the console
        consoleHandler = logging.StreamHandler() 
        consoleHandler.setFormatter(formatter)
        #add handlers to the logger
        logger.addHandler(consoleHandler)
        logger.addHandler(hdlr)
        logger.setLevel(logging.DEBUG) 
        return logger

In order to use this function:

    #first you set it up - you can use setuplogging as is below, to output to C:\tmp\Logging, or to any other path you choose

    logger = SetUpLogging(r"C:\tmp\Logging")

    #then you use it
    logger.debug("This is a DEBUG statement")
    logger.error("This is an ERROR statement")
    logger.info("This is an INFO statement") 

The output of those 3 calls would look like this:

    '''
    2018-05-30 21:23:02,957 DEBUG This is a DEBUG statement
    2018-05-30 21:23:02,959 ERROR This is an ERROR statement
    2018-05-30 21:23:02,960 INFO This is an INFO statement
    '''

Note that you don't need to create the output folder ahead of time, as the function will create it on its own if it doesn't already exist. In your script, put the definition in above the main, then call SetUpLogging(...) in your Main function or in your execution block, then just use logger.debug("..."), logger.error("..."), or logger.info("...") in place of arcpy.AddMessage("...").

Enjoy!

|improve this answer|||||
0

Python add-ins allow you to use the print statement. The output will display in ArcMap's Python console.

https://desktop.arcgis.com/en/arcmap/latest/analyze/python-addins/debugging-add-ins.htm

Another option is to use a pop-up dialog box provided by the pythonaddins module, e.g. pythonaddins.MessageBox('Invalid selection.', 'Error', 0).

https://desktop.arcgis.com/en/arcmap/latest/analyze/python-addins/the-pythonaddins-module.htm

|improve this answer|||||

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.