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:
type the following text, and hit Enter key.
import ctypes
type the following text, and hit Enter key.
ctypes.windll.user32.MessageBoxA(0, "Your text", "Your title", 1)
type the text again in the Python prompt shell
ctypes.windll.user32.MessageBoxA(0, "Your text", "Your title", 1)
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With