TK Game Demo


Starter Code

In the comp110/lessons directory, create a sub-directory named ls36_gui.

__main__.py

In the ls36_gui directory, add a file named __main__.py (dunderscored). Copy in the following starter code:

Game.py

Add another file named Game.py and copy in the following starter code:

Window.py

Add another file named Window.py and copy in the following starter code:

InputFrame.py

Finally, a the last starter code file named InputFrame.py, with the following starter code:

After Class Tutorial

In class we modified both Window.py and InputFrame.py. The place where we left off both, in case you were having errors, is what follows.

Window.py

InputFrame.py

Resetting the InputFrame

After the user makes a guess, their input is left in the text Entry field of InputFrame. This is handy for remembering what your last guess is, but when we start a new game it should reset to empty.

Let’s add some additional capability to the InputFrame class by introducing its own reset method:

The text attribute is an Entry object which has a delete method (in addition to many others for manipulating the current text in an input field). The start index is 0 and uses the special END constant, defined by Tk, to delete all. This kind of knowledge would be found by searching Google for “how to clear a Tk Entry in Python”.

Now, this method needs to be called after the user wins a game. So back in Window’s handle_guess method, try calling this method after resetting the model: self.input_frame.reset()

Play the game and win to convince yourself the text you previously entered into the Entry actually gets cleared.

Adding a ResultsFrame

Rather than popping up inforomation to the user after each guess in a window that has to be clicked away, let’s add a new kind of pane beneath the input pane that shows the outcome of a guess. When a guess is correct, we will also show the user a button they can press to play again.

Begin by adding a new file in the ls36_gui directory named ResultsFrame.py. It should have the following contents to begin with:

Now, we should add a ResultsFrame object to our Window. First, we need to import it:

Then, we establish an attribute of Window:

In the __init__ constructor, we must initialize the attribute:

Then, rather than having the handle_guess method of the Window class cause the messagebox popups, let’s rewrite handle_guess to update the Game state and then call the update function of the ResultsFrame:

Remember the connection here. When the user makes a guess, this handle_guess method is called. The game state is updated and then results_frame is updated so that its label changes.

If you play the game you should see the message change and ultimately be presented with a Button. How can we make the button click actually take some kind of action?

Reset the Game

Each Button has a special dictionary key attribute named "command". You can set the command of a button to be any function or method with 0-parameters and a return type of None. Ultimately, we want the Window to handle what it means for the game to be reset, so let’s setup a method in Window to reset the game first:

Notice each of the model and input frame’s respective reset methods were called, followed by the results_frame update method which is driven by the state of the Game.

Next, we want the ResultsFrame class to set the button’s command to call this reset method. To do so, let’s expand the constructor of ResultsFrame slightly.

Notice two changes: first, the third parameter added. Second the reset button’s "command" key attribute was updated to be linked to the reset handler.

Finally, in the Window constructor, you’ll need to update the construction of the ResultsFrame to include a reference to the reset method:

Now, if you play through the game, you should be able to reset it after you win and have the game go back to a start state.

Notice how the Window, ResultsFrame, and InputFrame attempt to separate their concerns from one another and divide up the work of building the interface and handling user inputs. This type of strategy is encouraged for building the GUI application for the next project. It takes some time to get comfortable with, but this pattern can work really well in building toward larger applications.

This completes the tutorial. The final versions of Window, ResultsFrame, and InputFrame are included below:

Final Versions

Final Window.py

Final InputFrame.py

Final ResultsFrame.py