Python Add-In Message Box interaction

4964
4
08-17-2015 08:43 AM
CoyPotts1
Occasional Contributor III

I have an add-in created, and it runs the tools as needed.  I would like to expand upon those tools to add some checks and balances with questions via the MessageBox function.  I can add message boxes just fine, but I'm trying to figure out how to script the *_addin.py file so that it does certain things based on the answer to the message box.

I have a simple message box shown below:

pythonaddins.MessageBox('Have you applied a definition query to all necessary layers?', 'Query check', 4)

This creates the Yes/No message box, and works just fine on its own.  The problem now is that I'm not sure how to make Yes run the tool, while No stops the tool. 

I've tried the following with no luck.

pythonaddins.MessageBox('Have you applied a definition query to all necessary layers?', 'Query check', 4)
     pythonaddins.GPToolDialog(toolPath, 'ToolNameHere')  # toolPath is declared at the top...works without the MB

and

mbAnswer = pythonaddins.MessageBox('Have you applied a definition query to all necessary layers?', 'Query check', 4)
     if mbAnswer = 'Yes':
          pythonaddins.GPToolDialog(toolPath, 'ToolNameHere')
     else:
          pass

I can't seem to find anything online regarding how to use the user inputs to determine what to do when clicked.  I've found stuff about "Tkinter" and stuff similar, but nothing about the pythonaddins.MessageBox option. 

Thanks!

0 Kudos
4 Replies
CoyPotts1
Occasional Contributor III

I can already see an error, but I'm not sure if it'll fix the issue.  I'm only using one equals sign for mbAnswer = 'Yes'.  I'll give that a go. 

0 Kudos
CoyPotts1
Occasional Contributor III

That didn't fix the issue.  All of my buttons (not just the one I'm modifying) show up as missing. 

0 Kudos
DanPatterson_Retired
MVP Emeritus

the suggested route would be to make the definition query a required parameter...then the tool will fail if not done correctly...skip the message boxes altogether

CoyPotts1
Occasional Contributor III

That would definitely be ideal...but I don't know how to do that either, unfortunately.  I didn't know you could do that.  Is the methodology just a lookup of the definition query value and then an error message if it doesn't exist?

I'll add a little as well.

Our feature classes have an initial query of "NetworkID IS NULL", so that whenever someone opens up our template map, they'll ideally see nothing.  They load the new project data, modify the fields in each feature class to match the NetworkID of their respective project, and then apply that definition query to each feature class to show only that NetworkID.

The problem is that not everyone remembers to do this.  When they get to a certain point where they are to run a script that calculates/runs a lot of fields/tools, they're going to be doing so to a much wider range of data than intended.

Since we already have a query applied to show NetworkID IS NULL, I am thinking something like this:

class tool_name(object):
     """Implementation for addin_name_addin.button_tool_name (Button)"""
     def __init__(self):
          self.enabled = True
          self.checked = False
     def onClick(self):
          for layer in layers:  # will define above
               if layer.isFeatureLayer:
                    if layer.name == 'SampleLayer1'
                         if layer.definitionQuery == """  'NetworkID = 'SampleProject' """  # I would like to add two options (as shown, and if == none)
                              pythonaddins.MessageBox('Modify the ' + SampleLayer + ' definition query and retry.', 'Incorrect Definition Query', 0)
                         elif:
                              pass
                    elif:  # going through the rest of my feature classes with this level of elif's
                         pass
               elif:
                    pythonaddins.GPToolDialog(toolPath, 'ToolName')     

I'm sure there are some issues with this, but it's just a stab in the dark.  I'm not sure if the placement of the tool run command is correct, and I'm not sure if my quoting around my definition query is correct either, just to name some that I have concerns with. 

0 Kudos