Run Python script from button on toolbar.

2666
3
Jump to solution
05-09-2019 10:13 AM
DaleShearer
Occasional Contributor

Thanks for looking at my question.

    I learning Python as I go and have made some headway, so here is what I have now ran into.   I have a Python script that creates a menu, I can run it from PyScripter and it all works.  

I have created a toolbar using the Python Add-In Wizard that contains a button that will be used to load/run/execute/display the menu.

What I want is to click the button, menu appears, then I work with the selections on the menu to work with the layers.

When I start ArcMap the toolbar is displayed with the button.  When I click the button I get a messagebox that I programmed in to make sure it works, one I clear the messagebox I get a quick flash on the screen that I cannot make out, and that is it.

This is the code for the button on the toolbar.

import arcpy
import pythonaddins
import os

class btnOpenMenu(object):
     def __init__(self):
            self.enabled = True
            self.checked = False
     def onClick(self):
            pythonaddins.MessageBox("Open Menu", "Layer Menu", 0)
            os.system('D:\Applications\Python\Open Menu.py')

I also tried:   os.system(r'D:\Applications\Python\Open Menu.py')   which did not work.

Thanks for your help.

0 Kudos
1 Solution

Accepted Solutions
DaleShearer
Occasional Contributor

In the end:

execfile("D:\Applications\Python\Open_Menu.py")

is what it took to open the menu.

import arcpy
import pythonaddins
 

class btnOpenMenu(object):
     def __init__(self):
            self.enabled = True
            self.checked = False
     def onClick(self):
            pythonaddins.MessageBox("Open Menu", "Layer Menu", 0)
            execfile('D:\Applications\Python\Open Menu.py')

View solution in original post

0 Kudos
3 Replies
DaleShearer
Occasional Contributor

Well, making progress, it now works, sorta.

Changed os.system to os.startfile('D:\Applications\Python\Open_Menu.py')

Now I get the cmd window (blank) with the heading: C:Python27\ArcGIS10.5\python.exe opening.

The cmd window opens right up, then about 5-10 seconds later my menu is displayed.

So now both are open, so now I need to get the cmd window from opening.

0 Kudos
DaleShearer
Occasional Contributor

And if I close the command window the menu closes......

0 Kudos
DaleShearer
Occasional Contributor

In the end:

execfile("D:\Applications\Python\Open_Menu.py")

is what it took to open the menu.

import arcpy
import pythonaddins
 

class btnOpenMenu(object):
     def __init__(self):
            self.enabled = True
            self.checked = False
     def onClick(self):
            pythonaddins.MessageBox("Open Menu", "Layer Menu", 0)
            execfile('D:\Applications\Python\Open Menu.py')

0 Kudos