Brian - thanks for your continuing help!  Here is ALL of the code.  Sure
hope you can help. Cheers. Diana

# Number 10
# A Guess the Number program
# Hawksworth - 28/3/05

from Tkinter import *
import random

class Application(Frame):
    """ GUI application  - guess the number game. """
    def __init__(self, master):
        """ Initialize the frame. """
        Frame.__init__(self, master)
        self.grid()
        self.bttn_clicks = 0    # the number of button clicks
        self.create_widgets()
        self.doRandom()
        self.guess = 0


    # a function to randomize a number
    def doRandom(self):
        self.number = random.randrange(10) + 1

    # a function that creates various buttons and text boxes
    def create_widgets(self):
        """ Create button, text, and entry widgets. """
        # create instruction label
        self.inst_lbl = Label(self, text = "Guess a number between 1 and
10.\n"
                              "You have 5 guesses to get it right!", fg =
"blue")
        self.inst_lbl.grid(row = 0,rowspan = 2, column = 0, columnspan = 2,
sticky = W)
        self.inst_lbl = Label(self, text = "Make a guess then press the
Enter key", fg = "red")
        self.inst_lbl.grid(row = 2,column = 0, columnspan = 2, sticky = W)

        # create label for number
        self.num_lbl = Label(self, text = "What's your Guess?: ", fg =
"blue")
        self.num_lbl.grid(row = 3, column = 0, sticky = W)

        # create entry (text)widget to accept number
        self.num_ent = Entry(self, width = 5)
        self.num_ent.grid(row = 3, column = 1, sticky = W)
        self.num_ent.focus_set()
        self.num_ent.bind('<Return>', (lambda event: self.two_commands()))

        # create submit button to call the appropriate message
        self.submit_bttn = Button(self, text = "Tries: 0", command =
self.two_commands)
        self.submit_bttn.grid(row = 4, column = 0, sticky = W)

        # create number of tries button
        """ Create button which displays number of clicks. """
        self.click_bttn = Button(self, text = "Tries: 0", command =
self.two_commands)
        self.click_bttn.grid(row = 4, column = 0, sticky = W)

        # create text widget to display message
        self.message_txt = Text(self, width = 35, height = 5, wrap = WORD)
        self.message_txt.grid(row = 5, column = 0, columnspan = 2, sticky =
W)

        # create reset and quit widgets
        self.bReset = Button(self, text="Reset", command=self.doReset)
        self.bReset.grid(row = 6, column = 1, sticky = W)

        self.bQuit = Button(self, text="Quit", command=self.doQuit)
        self.bQuit.grid(row = 7, column = 0, sticky = W)

   # quit the program
    def doQuit(self):
        self.quit()

   # restore default settings
    def doReset(self):
        self.doRandom()         # randomizing another number
        self.bttn_clicks = 0    # the number of button clicks
        self.tries = 0          # increasing the number of guesses
        self.create_widgets()   # returning to default settings

    # calling the functions to reveal the message
    # and to update the count button
    def two_commands(self):
        self.reveal()
        self.update_count()

    # updating the count button on user click
    def update_count(self):
        """ Increase click count and display new total. """
        self.bttn_clicks += 1
        self.click_bttn["text"] = "Tries: %s" %self.bttn_clicks
        if self.bttn_clicks > 5:
            self.toomany()

    def toomany(self):
        message = "Out of guesses! Your number was: %s" %self.guess + " The
number you wanted was: %s" %self.number
        #message += str(self.number)

        self.message_txt.config(state = NORMAL)
        self.message_txt.delete(0.0, END)
        self.message_txt.insert(0.0, message)
        self.message_txt.config(state = DISABLED)

    # revealing the message comparing the guess with the random number
    def reveal(self):
        """ Display message based on number. Changing text widget to integer
            Correcting the entry of a string variable - except the error
checking isn't working!"""

        try:
            self.guess = int(self.num_ent.get())

        except(ValueError),e:
            message = "%s is not a number. Please try again!" %self.guess

        else:
            self.num_ent.delete(0,END)
            self.num_ent.focus_set()

            if self.guess < self.number:
                message = "%s is too low! You need to guess higher. "
%self.guess

            if self.guess > self.number:
                message = "%s is too high! You need to guess lower."
%self.guess

            if self.guess == self.number:
                message = "%s is the correct number!" %self.guess

                self.message_txt.config(state = NORMAL)
                self.message_txt.config(state = DISABLED)

            self.message_txt.config(state = NORMAL)
            self.message_txt.delete(0.0, END)
            self.message_txt.insert(0.0, message)
            self.message_txt.config(state = DISABLED)


# main
root = Tk()
root.title("Guess the Number Game")
root.geometry("250x250")

app = Application(root)

root.mainloop()


_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to