5

I have found a very strange problem about using the python scripts under Python Windows command line prompt, to reproduce this issue, you can simply do those steps:

  1. start a Python command line prompt(this is usually to hit the Start Menu->Python 2.7->Python(command line).
  2. type the following text, and hit Enter key.

    import ctypes
    
  3. type the following text, and hit Enter key.

    ctypes.windll.user32.MessageBoxA(0, "Your text", "Your title", 1)
    
  4. You will see a message box opened, but this message box window is not activated.
  5. Use the mouse to click on the icon of the message box in task bar to activate the message box
  6. Close the message box
  7. type the text again in the Python prompt shell

    ctypes.windll.user32.MessageBoxA(0, "Your text", "Your title", 1)
    
  8. Now, the message box is showed activated (the expected behavior)

So, my question is, why the first message box(window) is not shown active? I originally find this issue when I run a Python pretty printer under GDB command line, because I want to use some python pretty printer to visual the data, like this GDB cv::Mat python object issue when debugging a c++ program, I need to show the OpenCV Image window immediately after I type the plot command.

But later I found that this is an issue related to Python itself.

1
  • 1
    I don't have time now to figure out how to do this, but have you checked out the reference for MessageBoxA? It says there is an option, MB_SETFOREGROUND that explicitly sets the dialog to be active. If it weren't modal, you could do it directly with ctypes.windll.user32.SetForegroundWindow(hwnd). I think the MB_SETFOREGROUND does exactly that internally. This page might help with figuring out how to set the option. Commented Dec 18, 2013 at 6:58

1 Answer 1

4

I realized the original answer wasn't actually activating the window as it should on the first try. However, this SO answer does work for me on the first try. Use the MB_SETMODAL flag (0x00001000) as a workaround:

set_modal_flag = 0x00001000
ctypes.windll.user32.MessageBoxA(0, "Your text", "Your title", set_modal_flag)

(*edit - previous code that maybe "should" work but doesn't.)

Can you try this? The 0x10 option is the MB_SETFOREGROUNDWINDOW option that I mentioned in the comments. Does it do what you want?

set_foreground_flag = 0x00010000
ctypes.windll.user32.MessageBoxA(0, "Your text", "Your title", set_foreground_flag)

As for "why" it doesn't work, the answer linked above also explains that. I had trouble activating the window as well in the past and used SetForegroundWindow() directly but on asynchronous windows where I could call it directly.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi, Kobejohn, I just try the method with the option: MB_SETFOREGROUND (0x00010000L), it did works, thanks! As you said, the remaining question is that if this option is not used, why the second time I run the command ctypes.windll.user32.MessageBoxA(0, "Your text", "Your title", 1), the window will show active?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.