Python Hangman Game with GUI – Guess the Words with Python

You must have played word guessing games in childhood. Let’s build the same game hangman using python

It will help you to improve your python implementation skills and also you will have a clear idea of strings and Tkinter.

Tkinter is a gui programming toolkit for Python. Let’s look at the prerequisites and module requirements.

Python Hangman Project Prerequisites

You just have to know the basics of Python and Tkinter to implement this project.

To install the required libraries, please use pip installer from the terminal:

pip install tkinter

Download Hangman Game Python Program

Please download the source code of python hangman: Hangman Game Python Program

Create Hangman Python File

Hangman_ProjectGurukul.py:

import random
from tkinter import *

print("-----------Welcome to ProjectGurukul------------")

root=Tk()
root.title('ProjectGurukul Hangman Game')

words = ['Project', 'Gurukul', 'Python', 'Hangman', 'Coding', 'Computer', 'Laptop', 'String', 'Guess', 'Tkinter' ]
greet = Label(root, font = ('arial', 30, 'bold'), text = "Welcome!").grid(row = 0,columnspan = 3)

won=0
entered=''

curr = random.choice(words)
print(curr)

guessed=''
moves=len(curr)+1
str=''

#This function contains the complete logic of hangman game
def play():
    global guessed
    global curr
    global won
    global entered
    global moves
    global str

    entered=f"{en.get()}"

    if(entered in guessed):
        btn=Entry(root,textvariable=en,width=5,font =('arial', 20, 'bold'))
        btn.grid(row=2,column=2)
        submitbtn=Button(root,text="Submit",command=play,bg="DodgerBlue2",fg="white",font = ('arial', 20, 'bold')).grid(row=4,columnspan=3)
        return

    guessed+=entered
    entered=entered[0]
    print(entered)

    won=1
    str=''
    for ch in curr:
        if ch in guessed:
            str+=ch
            print(ch,end = '')
        else:
            won=0
            str+='_'
            print("_",end = '')

    print('')    
    entered_string =Label(root, font = ('arial', 20, 'bold'),text=str).grid(row=6,columnspan=3)
    
    moves-=1
    print(moves)

    if(won==1):
        Win = Label(root, font = ('arial', 20, 'bold'), text = "You Won!!!")
        Win.grid(row = 8, columnspan = 3)

    elif won==0 and moves<=0:
        Lost = Label(root, font = ('arial', 20, 'bold'), text = "You Lost!!!")
        Lost.grid(row = 8, columnspan = 3)

    else:
        btn=Entry(root,textvariable=en,width=5,font =('arial', 20, 'bold'))
        btn.grid(row=2,column=2)
        submitbtn=Button(root,text="Submit",command=play,bg="DodgerBlue2",fg="white",font = ('arial', 20, 'bold'))
        submitbtn.grid(row=4,columnspan=3)


msg = Label(root, font = ('arial', 20, 'bold'), text = "Enter a character")
msg.grid(row = 2,column = 1)

# while won==0 and moves>0:
en=StringVar() 
btn=Entry(root,textvariable=en,width=5,font =('arial', 20, 'bold'))
btn.grid(row=2,column=2)

submitbtn=Button(root,text="Submit",command=play,bg="DodgerBlue2",fg="white",font = ('arial', 20, 'bold'))
submitbtn.grid(row=4,columnspan=3)

mainloop()

Explanation:

Function Used – Play(): This function contains the main logic of the python hangman game. It is actually a recursive function (function calling itself) which calls itself again and again until no more moves are left.

  • First, it checks if the user has re-entered any character by checking if the entered character is present in guessed string and if it is present, it calls itself again (i.e. asks the user to again enter another character) without decrementing the moves.
  • If that’s not the case, it generates current string from all the previously entered characters and displays it.
  • If the current string is complete, i.e. without any underscores(‘_’) the variable won will be 1, otherwise, it will be zero.
  • After that, if won is true(1) then it stops the execution and displays the winning message.
  • Else if the move is 0 i.e. No more moves are left, then it displays the required message.
  • Otherwise, it decrements moves and calls itself again.

Project Output

python hangman game

game won output

Summary

We have successfully completed python hangman project using just one simple function. In this hangman program, we have used tkinter and basic functions & modules of python.

Keep learning and developing more interesting projects, available on ProjectGurukul. Happy Coding !!!

Leave a Reply

Your email address will not be published. Required fields are marked *