Select to view content in your preferred language

Python addin toolbar running a python toolbox closing down ArcMap when run

4154
2
11-24-2015 02:54 AM
BenjaminSimpson
Deactivated User

Hi,

I am trying to build a python toolbox which is called from a python addin toolbar button in ArcMap. At the moment I am just trying to test the python toolbox to get it to work from the toolbar button. The problem I am currently having is that everytime I press the toolbar button ArcMap closed down/crashes with nothing else, such as an error message appearing.Below I have the code that I am currently using.

Toolbar Script:

import arcpy
import pythonaddins
import os
import sys

class AppendModelsOracle(object):
    """Implementation for Append_Models_Oracle.button (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        # Toolbox name
        toolboxName = "Toolbox"
        # Name of tool to be executed
        toolName = "Tool"
        # Create string with path to toolbox
        relPath = os.path.dirname(__file__)
        pyt = relPath + os.sep + toolboxName + ".pyt"
        # Call geoprocessing tool
        pythonaddins.GPToolDialog(pyt, toolName)
        pass

Python Toolbox Script:

import arcpy
import pythonaddins
import os
import sys
class Toolbox(object):
 def __init__(self):
  """Define the toolbox (the name of the toolbox is the name of the
  .pyt file)."""
  self.label = "Toolbox"
  self.alias = ""
  # List of tool classes associated with this toolbox
  self.tools = [Tool]
class Tool(object):
 def __init__(self):
  """Define the tool (tool name is the name of the class)."""
  self.label = "Tool"
  self.description = ""
  self.canRunInBackground = False
 def getParameterInfo(self):
  """Define parameter definitions"""
  model_list = arcpy.Parameter(
   displayName="Input GDB",
   name="in_gdb",
   datatype="DEWorkspace",
   parameterType="Required",
   direction="Input",
   multiValue="Yes")
   
  model_list.filter.type = "Workspace"
  model_list.filter.list = ["Local Database"]
  
  return [model_list]
 def isLicensed(self):
  """Set whether tool is licensed to execute."""
  return True
 def updateParameters(self, parameters):
  """Modify the values and properties of parameters before internal
  validation is performed.  This method is called whenever a parameter
  has been changed."""
  return
 def updateMessages(self, parameters):
  """Modify the messages created by internal validation for each tool
  parameter.  This method is called after internal validation."""
  return
 def execute(self, parameters, messages):
  """The source code of the tool."""
  model_list_len = len(model_list)
  model_list_pos = 0
  model_count = 1
  for model in model_list:
   msg1 = ("\nFirst model in list of selected models is: {0}".format(os.path.basename(model)))
   msg2 = ("\n{0} out of {1} models identified.".format(model_list_pos, model_list_len))
   arcpy.AddMessage(msg1 + msg2)
   model_list_pos = model_list_pos + 1
   arcpy.AddMessage(model_list_pos)
   model_count = model_count + 1
   arcpy.AddMessage(os.path.basename(model))
  arcpy.AddMessage("\nAll models identified." + "\nTest Successful.")
  pythonaddins.MessageBox("Bloody well work.","Test box 2",0) 
  return

Can anyone see a reason why my toolbar and/or toolbox would be causing ArcMap to shut down?

Thanks in advance for any help,

Ben.

0 Kudos
2 Replies
RebeccaStrauch__GISP
MVP Emeritus

Ben, I haven't worked with Python Toolboxes, but have with Python Addins, using a custom toolbox, which is slightly different.  My  <addin>_addin.py is similar to the Toolbar Script, to I'll include it below so you can see how I've have to deal with the relative path. And just fyi, for my addins, my structure is organized like the graphic below....again, I haven't made a python toolbox yet (but may look into it), so not sure if it is set up the same.

My ChkandFixLinks_addin.py

import arcpy
import pythonaddins
import os
relPath = os.path.dirname(__file__)
toolPath = relPath + r"\CheckAndFixLinks.tbx"

class btnGDBInventory(object):
    """Implementation for GDBInventory_addin.button (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        pythonaddins.GPToolDialog(toolPath, "ListFGDBsize")

class btnFCInventory(object):
    """Implementation for FCInventory_addin.button (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        pythonaddins.GPToolDialog(toolPath, "FCInventoryReport")

class btnListBrokenSources(object):
    """Implementation for ListBrokenSources_addin.button (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        pythonaddins.GPToolDialog(toolPath, "ListUniqueBrokenLinksNoFix")

class btnFixDriveLetters(object):
    """Implementation for FixDriveLetters_addin.button (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        pythonaddins.GPToolDialog(toolPath, "ListUniqueBrokenLinksWFix")

class btnDataSourceRepair(object):
    """Implementation for DataSourceRepair_addin.button (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        pythonaddins.GPToolDialog(toolPath, "RepairDetailedBrokenLinks")

class SpareButton1(object):
    """Implementation for SpareButton1_addin.button (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        pass

class YieldForInstructions(object):
    """Implementation for SpareButton2_addin.button (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        pass

I hope that helps.

Luke_Pinner
MVP Regular Contributor

I would remove all references to the pythonaddins module in your python toolbox. It's only for use in a python add in.