Play Hangman using python

Since I was bored , I wanted to write a script to build a simple version of Hangman using PyQt4. The logic (of course) wasn’t that difficult , however finding syntax for certain parts of the program was difficult , so I thought this post should save time , for those needing the syntax as well as me , later on . So here goes!

One has to import all the required libraries and call QtGui.QApplication to start the application.

app = QtGui.QApplication(sys.argv)

ex = Hangman()


The main requirements are , an on-screen keyboard , something to paint the Hangman , an edit box , a couple of labels . Half work done.

I would be focussing only a few methods (or) functions , which I had trouble in.

First , design your main window , with a simple label , and a qline-edit box , and a qpushbutton . You should place them in a container , vbox/hbox – layout (or) qgrid layour . I prefer qgrid layouts , since they are easier to understand . And for connecting a button to a function , you use this syntax


self.sender()  //and by this you get the button which calls the function

P.S : For more details on how to build a simple window , please refer to almost any of my previous posts , almost all of them would be related to GUI programming , because that’s all I know .

1 . Painting the hangman

I had defined a list called hangman list which consisted a list of seven zeros . Whenever a wrong input is given , a zero is turned into a one , and a part of the hangman is painted . For this a method called paintEvent is used. This involves QtPainter .

def paintEvent(self , e):

    qp = QtGui.QPainter()  //initialising the painter

    qp.begin(self) //starting the painter

    pen = QtGui.QPen(QtCore.Qt.Red , 5 , QtCore.Qt.SolidLine)  //       pen to draw lines ,  red makes it scary , 5 - thickness
    qp.drawLine(20 , 20 , 20 , 200)  //first two co-ordinates are the starting points and the lat two are the ending points
    qp.drawLine(20 , 20 , 120 , 20)

    qp.drawLine(20 , 20 , 120 , 20)

    if self.hangman[0] == 1:

        qp.drawLine(120 , 20 , 120 , 50)   //this is for the noose
    if self.hangman[1] == 1:

        point = QtCore.QPoint(120 , 70)
        qp.drawEllipse(point , 20 , 20)   //this is for the head

An important thing is to be noted here . Wherever self.hangman is changed , self.update() must be called. This makes sure that the painting is updated.

2 . OnScreen Keyboard.

The onscreen keyboard can be implemented using 26 different push buttons , and linking them to the same function. For using functions , when the user presses keys , a method called keyPressEvent is used.

def keyPressEvent(self , e):

    if e.key() == QtCore.Qt.Key_A:

This makes sure the given function gets executed when one presses A . If one wants to link the same function , when one
presses the key ‘Q’ and actually clicks the key ‘Q’ on the keyboard , what should be done. I have no idea in PyQt since
the syntax is quite different. However this code might help.

def keyPressEvent(self , e):

    if e.key() == QtCore.Qt.Key_A:

def fun(self , text):
    if text == False:
    '''This is the Push Button has default value of not checkable , and hence when clicked , False is passed'''
     \\implement function
    for i in range(self.layout.count()):
        w = self.layout.itemAt(i).widget()
        if isinstance(w , QtGui.QPushButton):
            if w.text() == text:
                \\implement function

And if all widgets have to be cleared , use

for i in range(self.layout.count()):

For the entire code please refer to this.

And these are the links I referred to .
This is how it looks.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: