win32con.MB_YESNO and win32con.MB_OK

1629
3
09-27-2012 08:48 AM
JohnLay
Occasional Contributor
I'm trying out pop-up message windows and am not having much luck. I want to ask the user if certain files exist before beginning a process then instruct the user to download the files if they don't already exist. I opted to throw up a message box that asks if the files exist using the win32api.MessageBox / win32con.MB_YESNO functions, then if the answer is 'no' another message box using win32con.MB_OK.

The script runs and a message box appears with the yes/no buttons, but nothing happens if you click either button. I actually have to click the 'Close' button on the main script dialog before only the 'no' button will work in the message box. But then I get a process canceled by user error.

While I'm sure that there is another way to do this, now that I've started down this path I'm determined to figure it out. Anybody got any ideas? I think I've checked every link on 7 pages of Google results and still have no idea what I'm doing wrong.

[HTML]import arcpy, win32com.client, win32api, win32con
from win32con import MB_OK, MB_YESNO

result = win32api.MessageBox(0, "Do the files exist?.", "Shapefiles", win32con.MB_YESNO);
if result == win32con.IDYES:
    pass
else:
    win32api.MessageBox(0, "Please downloaded and restart the script.", "Shapefiles", win32con.MB_OK);

arcpy.AddMessage("did this work?")[/HTML]
Tags (2)
0 Kudos
3 Replies
MathewCoyle
Frequent Contributor
Works fine for me. Not sure why you need the semicolon at the end of your lines. This isn't Java. What version of Python and win32 are you using?

Also, you do this
from win32con import MB_OK, MB_YESNO

But call it like this anyways?
win32con.MB_YESNO


Wait, are you trying to execute this as a script tool through ArcMap? I don't think that will work. Why not just have a checkbox in the tool window asking if it is downloaded? Or better yet, ask them to navigate to it using a file dialogue, or search for it in the script itself.
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Hi John,

I don't believe you be able to use pop-up message box when executing the script from a toolbox.  See the following post

One suggestion is you could create a check box within the tool to have the user specify whether the file exists.  You can set up a Parameter as a Boolean data type.  If the box is checked, have your code do something, if not, use the 'arcpy.AddMessage' to report that the user needs to download the files.
0 Kudos
JohnLay
Occasional Contributor
Mathew & Jake,

Thanks for the quick replies. I was just coming back to add additional info to this post about running the script from Catalog.

I discovered that if I hit either button often and fast enough it will complete because somewhere between the clicks the message box becomes the active dialog and the main script window becomes inactive allowing the script to finish its job. If only there was a way to do this pragmatically...

I kind of figured that I was going to have to find another way around this a couple hours ago. But like I said earlier, once I started down this path, I wanted to finish it because everything I'd read said this should work (BTW the semi-colons are remnants from experimentation I forgot to remove).

I guess I will just have the code check for the files and use AddWarning to instruct the user to download the files like you both suggested. A little disappointing, but at lease it will work.
0 Kudos