[Tkinter-discuss] Re: strange bind problem...

Martin Franklin mfranklin1 at gatwick.westerngeco.slb.com
Thu Sep 30 13:47:12 CEST 2004


In reply to myself!

On Thu, 30 Sep 2004 11:54:29 +0100, Martin Franklin  
<mfranklin1 at gatwick.westerngeco.slb.com> wrote:

>
> Hi All,
>
>
>
> Hope you can help I have written a quick HangMan game for my 6
> year old son - not me ;-)
>
> And it works great..... but when the word changes, after guessing
> all the right letters, I get an extra 'guess' at the start of the next
> word.  I guess you would have to run it to see what I mean...
>
> Sorry it's a bit long
>
>
> from Tkinter import *
> from string import letters, lowercase
> import random
> import time
> import os
> import sys
>
>
>
> FONT = "Courier 36 bold"
>
>
> class HangMan(Tk):
>      def __init__(self):
>          Tk.__init__(self)
>          self.title("HangMan")
>          self.bind("<Any-Key>", self.binder)
>
>          self.words = ["oyster", "toy", "destroy", "employ", "convoy",
>              "royal", "cowboy", "enjoy", "annoy", "voyage"]
>          random.shuffle(self.words)
>
>          f = Frame(self)
>
>          self.canvas = Canvas(f, background = "white", width = 300,  
> height=300)
>          self.canvas.pack(side = "left")
>
>          self.drawman()
>
>          self.letters = {}
>          lf = Frame(f)
>          col = 0
>          row = 0
>          for letter in lowercase:
>              l = Label(lf, text=letter, font=FONT)
>              l.grid(row=row, column=col)
>              self.letters[letter] = l
>              col = col + 1
>              if col == 6:
>                  row = row + 1
>                  col = 0
>          lf.pack(side = "left")
>          f.pack()
>
>          f = Frame(self)
>          self.theword = Label(f, text="_ _ _ _ _", font=FONT)
>          self.theword.pack(side="left")
>          f.pack(fill="x")
>          self.setword()
>
>      def setword(self):
>          self.guessed = []
>          self.currentword = self.words.pop()
>          self.theword.config(text="_ " * len(self.currentword))
>
>          for l in self.letters.values():
>              l.config(fg = "black")
>
>
>
>
>      def binder(self, event):
>          key = event.keysym
>          print "binder called", event.keycode, key
>          if key not in letters:
>              ## ignore
>              return "break"
>
>          if key not in self.guessed:
>              self.guessed.append(key)
>              self.strike(key)
>              self.showletter()
>

		   if self.showletter()=='complete':
                    return 'break' # or just return?

Here is the problem...   self.showletter()
should return 'complete' if the word is
complete so this function does not continue



>              if key not in self.currentword:
>                  self.shownext()
>                  return "break"
>          return "break"
>
>      def shownext(self):
>          ## draw the next part of the hang man
>
>          try:
>              id = self.bodyparts.pop()
>          except:
>              ## try this word again!
>              self.showword()
>              time.sleep(4)
>
>              self.drawman()
>              self.words.append(self.currentword)
>              self.setword()
>              return "break"
>          try:
>              self.canvas.itemconfig(id, outline="black")
>          except:
>              self.canvas.itemconfig(id, fill="black")
>
>
>      def showword(self):
>          self.theword.config(text=self.currentword)
>          self.update()
>
>
>
>      def showletter(self):
>          #~ print "showletter called"
>          out = []
>          for c in self.currentword:
>              if c in self.guessed:
>                  out.append(c)
>              else:
>                  out.append("_")
>
>
>          self.theword.config(text=" ".join(out))
>          self.update()
>          if "_" in out:
>              # keep going....
>              pass
>          else:
>              time.sleep(4)
>
>              self.drawman()
>              self.setword()
>
		   return 'complete'


Above neds to resurn 'complete' (or somthing)
to calling function

If anyone is interested in having this code drop me a line...

Cheers
Martin

-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/



More information about the Tkinter-discuss mailing list