How To Make Guess The Word Game In Python Using Tkinter
Steps:
- initializes a list of words to guess from.
- sets up the GUI components, including labels, textboxes, and buttons with their respective functions.
- Create The "displayWord" function to displays a word with some missing letters as underscores and randomly selects positions to hide letters.
- Create The "checkWord" function to checks if the entered word in the textbox matches the current word being guessed and updates the result label accordingly (either "Correct" or "Wrong").
- The "Start" button initializes the game, disables itself, and enables the "Next" button for progression.
- The "Next" button checks the current word, updates the result, and proceeds to the next word.
- When all words have been guessed, the "Start" button is re-enabled to restart the game.
Project Source Code:
import tkinter as tk
from tkinter import *
from tkinter import Tk
import random
root = Tk()
root.title("Guess The Word Game")
words_list = ["driver","signature","history", "response","president","highway",
"computer", "appartment", "forest", "chocolat", "lawyer"]
index = -1
lbl_word = tk.Label(text="Word", font=("Verdana",20))
textbox = tk.Entry(text="Guess", font=("Verdana",20), justify="center")
btn_next = tk.Button(text="Next", font=("Verdana",20), state = "disabled",
bg="#2980b9", fg="#fff", command = lambda : btn_next_command())
btn_start = tk.Button(text="Start", font=("Verdana",20), bg="#2980b9",
fg="#fff", command = lambda : btn_start_command())
lbl_result = tk.Label(text="Result", font=("Verdana",20), bg="Grey", fg="#fff")
lbl_word.grid(row=0, column=0, sticky="nsew")
textbox.grid(row=1, column=0, sticky="nsew")
btn_next.grid(row=2, column=0, sticky="nsew")
btn_start.grid(row=3, column=0, sticky="nsew")
lbl_result.grid(row=4, column=0, sticky="nsew")
# create a function to display the word
def displayWord():
if index == -1:
lbl_word['text'] = "Word"
textbox.insert(INSERT, "Guess")
else:
text_val = words_list[index]
pos1 = random.randint(0, len(text_val))
pos2 = random.randint(0, len(text_val))
pos3 = random.randint(0, len(text_val))
text_val = text_val[:pos1] + "_" + text_val[pos1+1:]
text_val = text_val[:pos2] + "_" + text_val[pos2+1:]
text_val = text_val[:pos3] + "_" + text_val[pos3+1:]
lbl_word['text'] = text_val
# create a function to check if the word entred is correct
def checkWord():
if textbox.get() == words_list[index]:
lbl_result['text'] = "Correct"
lbl_result['bg'] = "Green"
else:
lbl_result['text'] = "Wrong"
lbl_result['bg'] = "Red"
if index == len(words_list) - 1:
btn_start['state'] = 'normal'
btn_next['state'] = 'disabled'
textbox.delete(0, END)
# create a function for the start button
def btn_start_command():
global index
index = 0
displayWord()
textbox.delete(0,END)
btn_start['state'] = 'disabled'
btn_next['state'] = 'normal'
lbl_result['text'] = ''
lbl_result['bg'] = 'Grey'
# create a function for the next button
def btn_next_command():
global index
checkWord()
if index < len(words_list) - 1:
index += 1
displayWord()
displayWord()
root.mainloop()
////// OUTPUT :