I am trying to catch a close event on multiple windows, but only one window catches the event and the second window only catches event after closing the first window. How can I catch this event on both windows independantly (This happens for other events such as resize as well)
I suppose you need to check the status of all windows through a loop, in other words, iterate through the list of windows and check the is_closed() flag for each
@serega-kkz Thank you for your response. I didn’t get it, is_closed() returns always false because when I click the close window nothing happens and my callbacks are not called. Could you elaborate please, maybe with an example?
I’m on vacation now, I can’t provide an example. However, this method will return the true flag if you close the window, I did not say that this would make callbacks work, I suggested changing the approach. You need to check this flag every frame. The only problem is that you need to create a main loop independent of open windows. Something like a server.
Thanks @serega-kkz, I tried your suggestion (I didn’t get what you mean by server and independant main loop, I simply added my control inside AsyncTask), but here is what happens: When I click close button on the second window, nothing happens (is_closed() is false and event callback is not called and window remains open). When I click close button on the first windows, close callback for first window is called (is_closed() is false).
If I remove the callbacks, here is what happens: When I click close button on the second window, nothing happens and window remains open (is_closed() is false). When I click close button on the first windows, it closes (graphics window doesn’t exist so I can’t test is_closed() on it). After the first window closes, when I click close button on the second window, it closes. I post the complete simple example below. I appreciate if you could help me when you have time.
It’s really weird, in python this call works as expected.
from direct.showbase.ShowBase import ShowBase
class Game(ShowBase):
def __init__(self):
ShowBase.__init__(self)
base.open_window()
base.open_window()
base.taskMgr.add(self.do_stuff, "do_stuff")
def do_stuff(self, task):
for i in range(base.win.get_engine().get_num_windows()):
if base.win.get_engine().get_window(i).is_closed():
print("WINDOWS", i ," CLOSED")
return task.cont
app = Game()
app.run()
I don’t seem to have any idea at the moment why this isn’t working.
I could be wrong, but my guess is that is_closed() will not return true unless the window is closed, which it won’t be at the time the close request event is fired (in fact, it’s your responsibility to actually close the window). If it is returning true then that’s because some other hook in Panda has already closed the window.
You can use a lambda capture or the user data parameter to communicate to the callback about which window is being closed.