Skip to content
Related Articles

Related Articles

PyQt5 – Jumble Word Game
  • Last Updated : 03 Jul, 2020

In this article we will see how we can create a jumble word game using PyQt5. Jumbled word game : Jumbled word is given to player, player has to rearrange the characters of the word to make a correct meaningful word. Below is how game will look like

GUI implementation steps :
1. Create a heading label that display the game name
2. Create label that shows the jumbled word
3. A line edit widget that get the text
4. A push button that check the input text which is beside the line edit
5. Create a result label that tells if answer is correct or not and change its color
6. Two push button to start and reset the game

Back end implementation steps :
1. Create a list of words
2. Add action to the check button
3. Inside the action of check push button get the input text and compare it with the input word
4. If word matches show result as correct and set color to green else set color to red and say wrong
5. Add action to the start button
5. Inside the start button action get the current word from the list using random function
6. Create a jumble word from the current word and set it to the jumble label
7. Remove the text of the result label and set color to yellow
8. Add action to the reset button
9. Inside the reset button action set the current word to blank
10. Remove text from all labels and set their color back to original one

Below is the implementation

# importing libraries
from PyQt5.QtWidgets import * 
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import * 
from PyQt5.QtCore import * 
import random
import sys
class Window(QMainWindow):
    def __init__(self):
        # setting title
        self.setWindowTitle("Python ")
        # setting geometry
        self.setGeometry(100, 100, 320, 350)
        # calling method
        # showing all the widgets
        # words
        self.words = ['red', 'cold', 'hot', 'geeks', 'rain'
                           'black', 'snow', 'hills', 'code']
        # current word
        self.current_text = ""
    # method for components
    def UiComponents(self):
        # creating head label
        head = QLabel("Jumbled Word Game", self)
        # setting geometry to the head
        head.setGeometry(20, 10, 280, 60)
        # font
        font = QFont('Times', 15)
        # setting font to the head
        # setting alignment of the head
        # setting color effect to the head
        color = QGraphicsColorizeEffect(self)
        # creating label to show the jumbled word
        self.j_word = QLabel(self)
        # setting geometry
        self.j_word.setGeometry(30, 80, 260, 50)
        # setting style sheet
        self.j_word.setStyleSheet("border : 2px solid black; background : white;")
        # setting font
        self.j_word.setFont(QFont('Times', 12))
        # setting alignment
        # creating a line edit widget to het the text
        self.input = QLineEdit(self)
        # setting geometry
        self.input.setGeometry(20, 150, 200, 40)
        # setting alignment
        # creating push button to check the input
        self.check = QPushButton("Check", self)
        # setting geometry
        self.check.setGeometry(230, 155, 80, 30)
        # adding action to the check button
        # result label
        self.result = QLabel(self)
        # setting geometry
        self.result.setGeometry(40, 210, 240, 50)
        # setting font
        self.result.setFont(QFont('Times', 13))
        # setting alignment
        # setting style sheet
        self.result.setStyleSheet("border : 2px solid black; background : yellow;")
        # creating push buttons to start and reset the game
        start = QPushButton("Start", self)
        reset = QPushButton("Reset", self)
        # setting geometry to both the button
        start.setGeometry(15, 290, 140, 40)
        reset.setGeometry(165, 290, 140, 40)
        # adding action to both the buttons
    def check_action(self):
        # getting text from the line edit
        text = self.input.text()
        # checking if text is similar to the current text
        if text == self.current_text:
            self.result.setText("Correct Answer")
            # making result color grenn
            self.result.setStyleSheet("background : lightgreen;")
            self.result.setText("Wrong Answer")
            # making result color red
            self.result.setStyleSheet("background : red;")
    def start_action(self):
        # selecting one word
        self.current_text = random.choice(self.words)
        # sample() method shuffling the characters of the word
        random_word = random.sample(self.current_text, len(self.current_text))
        # join() method join the elements
        # of the iterator(e.g. list) with particular character .
        jumbled = ''.join(random_word)
        # setting text to the jumbled word
        # setting result text to blank
        # making result label color yellow
        self.result.setStyleSheet("border : 2px solid black; background : yellow;")
        # setting text of input to blank
    def reset_action(self):
        # setting current text blank
        self.current_text = ""
        # setting text of input to blank
        # clear the text of all the labels
        # making result label color yellow
        self.result.setStyleSheet("border : 2px solid black; background : yellow;")
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app

Output :

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :