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")
Salah
  • 101
  • 2
  • 2
  • 12
  • *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*. – CristiFati Aug 25 '23 at 07: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. – Salah Aug 25 '23 at 07: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)](https://stackoverflow.com/a/58611011/4788546) for a common pitfall when working with *CTypes* (calling functions). – CristiFati Aug 25 '23 at 07:37
  • I'm already logging the data to file. But as per the requirement, this feature is needed. – Salah Aug 25 '23 at 07:42

1 Answers1

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.

5px
  • 131
  • 9
  • I suppose using thread will affect the program performance. any other method? – Salah Aug 25 '23 at 07:23
  • Check [\[SO\]: C function called from Python via ctypes returns incorrect value (@CristiFati's answer)](https://stackoverflow.com/a/58611011/4788546) for a common pitfall when working with *CTypes* (calling functions). – CristiFati Aug 25 '23 at 07:40
  • @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)](https://stackoverflow.com/a/58267245/22363405). Tk does the same thing when making multiple windows, but it uses multiprocessing instead which is SLOWER than threading when using ctypes (see [this](https://stackoverflow.com/questions/63736883/using-pythons-threading-module-to-call-ctypes-function-is-faster-than-using-mul)) – 5px Aug 25 '23 at 07:56
  • Can we do this without threading or multiprocessing? I just need some information window which should be present on the screen always. – Salah Aug 25 '23 at 08:09
  • @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. – 5px Aug 25 '23 at 08:36