0

If I use ctypes to show message box in python console (without Tkinter), I'm not able to show two boxes at a time. I've to close the first box, then only second box will appear.

What change should I made here? or is there any other library to serve this purpose?

def display_box(text):
    return ctypes.windll.user32.MessageBoxW(0, text, "title", 0)

display_box("first box")
display_box("second box")
4
  • MessageBox displays a modal dialog, meaning that the user should deal with it before anything else (including displaying another). Why do you need this behavior? Your question seems an XY Problem. Commented Aug 25, 2023 at 7:06
  • I needed this to display some results in an automation program. I want the boxes to be open until the program finishes. It should not wait for the user input. Commented Aug 25, 2023 at 7:20
  • Then probably the best way would be to log them (on screen, file, ...), so that they don't go away once user closes the box. Also, check [SO]: C function called from Python via ctypes returns incorrect value (@CristiFati's answer) for a common pitfall when working with CTypes (calling functions). Commented Aug 25, 2023 at 7:37
  • I'm already logging the data to file. But as per the requirement, this feature is needed. Commented Aug 25, 2023 at 7:42

1 Answer 1

2

You'll have to run your function asynchronously.

import ctypes
import threading

def display_box(text):
    return ctypes.windll.user32.MessageBoxW(0, text, "title", 0)

t1 = threading.Thread(target=display_box, args=("first box",))
t2 = threading.Thread(target=display_box, args=("second box",))

t1.start()
t2.start()

t1.join()
t2.join()

Returns both boxes on top of each other layered on top of each other. To change that modify your X and Y variables in the messagebox function.

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

5 Comments

I suppose using thread will affect the program performance. any other method?
Check [SO]: C function called from Python via ctypes returns incorrect value (@CristiFati's answer) for a common pitfall when working with CTypes (calling functions).
@Salah Not when using ctypes. You're actually making a call to the Win32 API, which is mostly synchronous as a whole, and definitely synchronous when making a modal window. (more info). Tk does the same thing when making multiple windows, but it uses multiprocessing instead which is SLOWER than threading when using ctypes (see this)
Can we do this without threading or multiprocessing? I just need some information window which should be present on the screen always.
@Salah Calling win32 isn't particularly CPU heavy so the threading approach should have a minimal to unnoticeable performance penalty. If you don't care about the order in which the boxes are shown you can remove the join for an extra speed boost.

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.