opening the python window from a custom toolbar

3518
2
Jump to solution
11-13-2015 05:42 AM
BenjaminSimpson
New Contributor III

Hi,

I am currently in the process of creating a custom toolbar containing 4 buttons in Arc. These buttons will run python scripts which mostly have the purpose of transfering data from one or more personal geodatabases to an oracle database.

The question I have is, is there a way to open the python window from within the python script? So that when the users presses one of the buttons the python window automatically opens and then the multiple print statements I have throughout the script will be displayed. It would probably be better to use the pythonaddins.MessageBox function but for the time being I will settle for displaying the messages from the python window.

Thanks in advance for any help,

Ben.

0 Kudos
1 Solution

Accepted Solutions
RebeccaStrauch__GISP
MVP Emeritus

You can use the Arcpy.AddMessage() to print in the Results window.  If you have Run-Process-in-Background turned off (unchecked) the process window will remain open and you will see what you print this way, or you can always look at the Results tab,   You can also creat a log file if you want that is separate from the results tab.

View solution in original post

2 Replies
RebeccaStrauch__GISP
MVP Emeritus

You can use the Arcpy.AddMessage() to print in the Results window.  If you have Run-Process-in-Background turned off (unchecked) the process window will remain open and you will see what you print this way, or you can always look at the Results tab,   You can also creat a log file if you want that is separate from the results tab.

FreddieGibson
Occasional Contributor III

I just tested this and most of it appears easily possible. Below is a screenshot that shows that I was able to get this to work. You'll see in the addin that I was about to included the out-of-the-box python window button on my toolbar. With my example I had to separate this workflow into two steps. The user has to click the Python Window command to show the window and it appears that the print statements will show up in this window.

pythonaddin.png

If I were to use ArcObjects I could the window to launch when clicking on the button. I'd assume in python that you'd need to use the comtypes library to interact with ArcObjects directly. Within the python code you'd need to grab an instance of the Python Window and launch it prior to executing your code. You can find the command using the guid for the python window as shown below.

ICommandBars commandBars = ArcMap.Application.Document.CommandBars;
UID commandID = new UIDClass { Value = "{1A7E7146-BDFB-4755-93DE-100171382BFF}" };
ICommandItem commandItem =  commandBars.Find(commandID, false, false);

commandItem.Execute();

To be honest, if I had to implement this workflow and didn't require any user interaction with the command I would honestly just run this against the geoprocessing framework as Rebecca Strauch, GISP suggested. The geoprocessing framework should provide you with everything you'd need to present to the user and would aid in helping you not block the UI thread when executing your task.