How to make a gui game in python

Get Started with Python GUI App by Building a Simple Guessing Game

How to make a gui game in python

I’m going to show you how to build a GUI-based guessing game in Python using Tkinter — which is a Python library for creating GUI widgets (buttons, labels, images, text area etc). This simple app will allow you to “get your feet wet” with core Python concepts/constructs. You can clone or download the complete source code on github. For more information about Tkinter and how to set it up please see Tkinter documentation: https://docs.python.org/3/library/tk.html.

For this tutorial, I am going to assume that you have setup Tkinter as well as Python. So, let’s get started!

The movie below shows you an example game play session for our awesome Guessing Game App.

Let’s dive into the code. We’ll start with the GUI components and later move on to the actual game logic. The code snippet below shows the main Game Window.

Fig.2: Code snipping for the Main Game Window

The code is hopefully self-explanatory :) The Tkinter library is imported in line 1 and the library instance is created in Line 3. The window title e.g. “Guessing Game” is set in Line 4. Now we created “widgets” (labels) in Line 6 and 7 to add to the window. Note that the widgets are not automatically added after creation. Line 10 and 11 add the created labels to the window. Note that the grid method is used to layout the Widgets in rows/columns. The main loop in Line 11 is an infinite loop as it keeps the app running until the player closes the app window (x). Fig.3 below shows the output of the code snippet (Fig.2.)

How to make a gui game in python

Fig. 3. Two Widgets (Labels) showing on the Main Window

Now that we have a basic understanding of how to create Widgets, let’s add the remaining Widgets to the window. Fig.4 shows the code snippet while Fig. 5 shows the corresponding output.

Fig.4: Code snippet for Main Game Window with all Widgets Added

How to make a gui game in python

Fig. 5: Main Game Window with all Widgets Added

Now that we have all the needed Widgets added to the Main Game Window, let’s focus on the actual game logic.

Fig. 6 below shows the entire code for this simple game. First, the “guess buttons” (i.e. buttons with text 0 to 9), for player guesses, are all enabled when a player clicks on the “Start Game” button — Line 118 to 119. When that happens, the “Start Game” button text changes to “Restart Game” — Line 121 to 125. This allows players to restart the game whenever they want. Also, the code automatically generates a “secret” random number between 0 and 9 (all inclusive) — Line 49.

Now players have three (3) attempts to guess the secret number by clicking on the guess button. If the guess was correct (guess is equal to secret number), then (1) the “totalNumberOfGuesses” variable is incremented and the label i.e. “lblNoGuess” text is updated according (2) A label stating that the “guess was correct…” (green colour) is displayed on the window and the Game session ends (the guess buttons are disabled when the game session ends) — Line 78 to 85.

The player can click on the “Restart Game” button to restart the Game. All variables are reset when that button is click — Line 126. On the other hand, if the player guess is not equal to the secret number, a “message hint” is displayed on the Window (in red colour — danger! ) — Line 88 to 97.

For example, if we assume the secret number is 5 and the player guessed 8. Then, a “message hint” stating that “Secret number is less than your current guess” is displayed (who doesn’t like a help hint!). Also, the total number of guesses is incremented. The game ends when the total number of guesses is equal to the maximum number of allowable guesses (i.e. static 3. Get ready to make this dynamic later!) — Line 101 to 110.

Fig. 6: Complete source code for the simple game

Fig. 7 through Fig. 11 show an example game play session. Here, the secret number was 7.

How to make a gui game in python

Fig. 7: Game Window before the start button is clicked. The “Guess buttons” are disabled

How to make a gui game in python

Fig. 8: The “Start Game” button is clicked and the “Guess buttons” are enabled

How to make a gui game in python

Fig. 9: First player guess is wrong. Player is notified and selected button is disabled

How to make a gui game in python

Fig. 10: Second guess is equally not the secret number

How to make a gui game in python

Fig. 11: Victor at last :) The “Guess buttons (0 to 9) are disabled and game is ended. Player can restart the game.

I have presented how to program a simple guessing game using Python and the Tkinter library in this tutorial. It introduces the reader to many Python concepts such as “variables, datatypes, if/else statements, loops, list, comments, functions, and the Tkinter library for implementing GUI.” It is my hope that cute little tutorial helps you get started programming in Python.

Here are some suggestions for improving the application. Let’s say your programming assignment: Implement additional features to:

  1. Allow player set the guess range e.g. from 10 to 20
  2. All user to set the max number of guess. It’s currently set to 3 and not changeable
  3. Dynamically create the “guess buttons” buttons depending on 1.
  4. Dynamic Count down timer!

Good luck!

Can I use Python to build a game?

Therefore, Python is also an ideal language to begin your adventure with creating games, regardless of whether you're already fluent in it or just starting out. Due to the newcomer-friendly syntax of Python, developers can focus on the basics of game programming, not the complexity of the language itself.

Can I use Tkinter to make a game?

TKinter is widely used for developing GUI applications. Along with applications, we can also use Tkinter GUI to develop games.

Is pygame good for GUI?

pygame is - as it's name states - a package designed to allow to create games in python very easily, not to create GUIs. As such it provides event handling, graphics sound bindings and some other things useful to write games, and it's written on top of SDL. So if you want to write a GUI-application, don't use pygame.