[Tutor] Hangman GUI

Alan Gauld alan.gauld at btinternet.com
Sun Dec 11 17:33:35 CET 2011


On 12/12/11 05:49, Aswin Shan wrote:


Please use a sensible subject line in your postings.
It will help people find the thread and encourage those with appropriate 
skills to read it.

> I need help in creating a GUI for my python code to program a Hangman. I
> have programmed a working code of Hangman, but I need to make it to a
> proper program with GUI.

The first thing you need to do is restructure it to separate out the 
logic from the UI. Then modify it to be event driven. Then choose a GUI 
framework (like Tkinter or wxPython) to actually build the GUI bit.


> import random;
> import time;
> correct_guesses = ['-', ' ']
> guessed_letters = []

> def Input_Error(input):
> if input.isdigit() == True:
> print "Input Error. \nIt's Hangman. Your secret word only includes
> letters. "
> while input.isdigit() == False:
> input = raw_input('Guess a letter: ')
> if len(input)>1:
> print 'Only enter one guess at a time'

You need to remove all the print statements since they won't work in a 
GUI. return strings instead so that the GUI framework can call the 
function and then display the result in the GUI.

> def Get_Random_Word():
> global word
> word_list = []
> for line in open('dictionary.txt'):
> word_list=line.split()
> wordno=random.randint(0, len(word_list) -1)
> word= word_list[wordno]
> print word
> return word

Same here, remove the print. I assume in this case its only for 
debugging anyhow!

> def displayBoard():
> display = []
> i = 0
> while i < len(word):
> if word[i] in correct_guesses:
> display.append(word[i])
> if word[i] not in correct_guesses:
> display.append('_ ')
> i +=1
> for w in display:
> print w,

O)bviously this will need to be rewritten using the GUI framework of 
your choice.


> def play():

This will dissappear and be replaced by events from the GUI.

> global player_guess
> global guess
> player_guess = (raw_input('\nGuess a letter: ')).lower()
> Input_Error(player_guess)
 > guess = 0
 > while guess < 9:
 > print guess
 > if player_guess.lower() in guessed_letters:
 > print "You have guessed this letter already"
 > elif player_guess in word:
 > guessed_letters.append(player_guess)

So this will be replaced by a user typing a value into a field
(or selecting a letter from a set of buttons?) and as a result calling 
an event handler to check for errors. Then checking valid inputs for an 
outcome...


> correct_guesses.append(player_guess)
> elif player_guess not in word and player_guess.isdigit()== False:

You don't need the isdigit since you already checked that in 
Input_Error() above

> guessed_letters.append(player_guess)
> print 'wrong'
> guess += 1
> if len(correct_guesses)-2 == len(word):
> print word
> print 'Congratulation, you guessed the word correctly in', guess, 'guesses.'
> break
> if guess == 8:
> break

And this stuff will all be replaced with code that updates results 
fields or labels/diagrams on the GUI.


> displayBoard()

This will happen automatically when the board changes.

> player_guess = (raw_input('\nGuess another letter: ')).lower()
> Input_Error(player_guess)


> def Welcome():
> print """ | | | | /\ | ___ \ / _____) ___ \ /\ | ___ \
> | |__ | | / \ | | | | / ___| | _ | | / \ | | | |
> | __)| |/ /\ \| | | | | (___) || || |/ /\ \| | | |
> | | | | |__| | | | | \____/| || || | |__| | | | |
> |_| |_|______|_| |_|\_____/|_||_||_|______|_| |_|
> Welcome to Hangman v1.0
> Rules:
> 1. You will have 8 chances to guess the letters correctly.
> 2. For each wrong guess one chance will be decremented.
> 3. If you guess the same word again, the chances will not be decremented.
> Good luck."""
> print "Generating your secret word..."
> time.sleep(3)

And this will be built uinto a welcome window/splash-screen.


> I also find trouble with the some hangman pic which I have provided
> below.

You should probably use images in the GUI version rather than draw the 
picture using ASCII art...

You can find more info on event driven coding in my tutor under that 
heading.

And you can get basic info on GUI programming in the tutor too.

If you can find a dead-tree version of my tutorial (library?) it has a
chapter with a Tkinter version of hangman as an example program.

HTH.
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list