Python: show message "Waiting for player ..." while socket is listening on connection

Creating a very simple tic-tac-toe game in Python using a socket-based P2P architecture. Currently, my GUI has a button that says “Create,” which opens and draws a new playing field window, creates a socket, binds, listens, and accepts the connection. The "Join" button will open and draw a new game panel and connect to this "server".

I try to show him the message "Waiting for a player ..." when you create a game, the cancel button to stop and return to the main menu, and make it disappear on its own if the connection was accepted.

I tried using tkMessageBox, but the script stops until the user clears the message, so I don't need to listen / receive until the user clicks something.

What other way to fulfill for me?

Thanks!

+5
source share
1 answer

Sounds like a thread problem.

I am not familiar with the TK graphics, but I imagine what you need to do is to launch a window with the message "Waiting for a player". Then this window stops, waiting for something.

When the message box appears, you need to “listen” to another thread that returns to the main message box when someone connects using a semaphore or queue.

In your main GUI thread, you need to do a loop:

  • Check the values ​​of the queue or semaphore. If there is value that you expect, close the window. This should be non-blocking, so that the GUI thread can still check for user input.
  • check user login. Perhaps this is done using callback functions.
+1
source

All Articles