Creating a Hangman Game using Python Tkinter – Tutorial for Beginners #shorts

Posted by


In this tutorial, we will be creating a simple Hangman game using Python and Tkinter. Hangman is a classic word guessing game where the player must guess a word by guessing individual letters. For each incorrect guess, a part of the hangman is drawn. The player has a limited number of guesses before the hangman is complete and they lose.

Before starting this tutorial, make sure you have Python installed on your computer. If you don’t have Python installed, you can download it from the official Python website. You will also need to have Tkinter installed, which is included with Python by default.

Let’s get started by creating a new Python file and importing the necessary modules:

import tkinter as tk
from tkinter import messagebox
import random

Next, we will define some variables that will be used in our Hangman game:

word_list = ['python', 'hangman', 'programming', 'computer', 'keyboard', 'mouse']
word = random.choice(word_list)
guessed_letters = []
num_attempts = 0
max_attempts = 6

Now, let’s create the main function that will handle the game logic:

def check_guess(letter):
    global num_attempts
    global max_attempts
    global guessed_letters

    if letter in guessed_letters:
        messagebox.showinfo("Hangman", "You've already guessed that letter!")
        return

    guessed_letters.append(letter)

    if letter not in word:
        num_attempts += 1

    if num_attempts == max_attempts:
        messagebox.showinfo("Hangman", "You ran out of attempts! The word was: {}".format(word))
        reset_game()
    elif all(letter in guessed_letters for letter in word):
        messagebox.showinfo("Hangman", "Congratulations! You guessed the word.")
        reset_game()
    else:
        update_display()

def reset_game():
    global word
    global guessed_letters
    global num_attempts

    word = random.choice(word_list)
    guessed_letters.clear()
    num_attempts = 0
    update_display()

def update_display():
    display_word = ''.join(letter if letter in guessed_letters else '_' for letter in word)
    label.config(text=display_word)
    attempts_label.config(text="Attempts: {}/{}".format(num_attempts, max_attempts))

Now, let’s create the GUI for our Hangman game using Tkinter:

root = tk.Tk()
root.title("Hangman")

label = tk.Label(root, text='', font=('Helvetica', 24))
label.pack()

attempts_label = tk.Label(root, text="Attempts: 0/6", font=('Helvetica', 12))
attempts_label.pack()

for letter in 'abcdefghijklmnopqrstuvwxyz':
    button = tk.Button(root, text=letter, command=lambda l=letter: check_guess(l))
    button.pack(side=tk.LEFT)

reset_button = tk.Button(root, text="Reset", command=reset_game)
reset_button.pack()

update_display()

root.mainloop()

That’s it! You’ve just created a simple Hangman game using Python and Tkinter. You can now run your program and start playing the game. Have fun guessing the words and have a great time with your new Hangman game!

I hope you found this tutorial helpful and informative. If you have any questions or run into any issues, feel free to leave a comment below. Thank you for reading!

0 0 votes
Article Rating

Leave a Reply

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@SalowStudios
1 day ago

Checkout the Full Code – https://github.com/Salow-Studios/HangMan-Using-Pygame

1
0
Would love your thoughts, please comment.x
()
x