Search code examples
multithreadingwxpython

wxpython: wait for events mainloop freezes


I am new to wxPython, so please be gentle. I am trying to make a game using wxPython. I need to be able to handle events (button clicks) while the game is in progress.

The process is:

  1. Deal the cards
  2. Wait for user input
  3. Continue accordingly

The way I have implemented it is:

app = wx.App()
g = Game() # calls g.Play() which executes the process above
app.Mainloop()

However the application freezes. I think the problem relates to being unable to respond to events while the process is being executed. How can I get around this?

I had a look at threading, but cannot see how to make this work in my case. If I create a new thread to deal with user inputs from within Game(), that will not be able to update the values in Game().

I am sure there is a "correct" way of doing this which I don't know because I am unfamiliar with wxPython. Can anyone help?


Solution

  • Yo do not need a seperate function play() to run the game. Just set up the event handlers to compute the state of the game during every event which results in a move of the game.

    A good option would be to define a game state as say the cards in each players hands, and the turns that have been played and the scores, all defined as an object of a state class.

    Chalk out an outline of how your game's architecture first. And you might also want to take a look at some examples and documentations on wxPython if you are new to it.