[Tutor] Python GUI

priyesh raj raj.priyesh at gmail.com
Wed Jun 29 07:11:20 CEST 2011


>
> Message: 2
> Date: Wed, 29 Jun 2011 16:28:25 +1200
> From: David Merrick <merrickdav at gmail.com>
> To: tutor at python.org
> Subject: [Tutor] Python GUI
> Message-ID: <BANLkTinuin4LV=6HE26TC2ysXHvQfMdk+w at mail.gmail.com>
> Content-Type: text/plain; charset="utf-8"
>
> # Guess My Number GUI
> # Create a story based on user input
>
> from tkinter import *
> import random
> class Application(Frame):
>    """ GUI application that creates a story based on user input. """
>    def __init__(self, master):
>        """ Initialize Frame. """
>        super(Application, self).__init__(master)
>        self.grid()
>        self.create_widgets()
>
>    def create_widgets(self):
>        """ Create widgets to get story information and to display story.
> """
>        # create instruction label
>        Label(self,
>              text = "Welcome to 'Guess My Number'!\n\nI'm thinking of a
> number between 1 and 100.\nTry to guess it in as few attempts as possible."
>              ).grid(row = 0, column = 0, columnspan = 2, sticky = W)
>
>
>        # create a label for body parts radio buttons
>        Label(self,
>              text = "Take a guess:"
>              ).grid(row = 6, column = 0, sticky = W)
>        self.numberEnt = Entry(self)
>        self.numberEnt.grid(row = 6, column = 1, sticky = W)
>
>        # create a submit button
>        Button(self,
>              text = "Click to see if you got it",
>               command = self.testNumber
>               ).grid(row = 7, column = 0, sticky = W)
>
>        self.numberTxt = Text(self, width = 75, height = 10, wrap = WORD)
>        self.numberTxt.grid(row = 8, column = 0, columnspan = 4)
>
>    def testNumber(self):
>        """ Fill text box with new story based on user input. """
>        # get values from the GUI
>
>        # create the story
>
>        guess = int(self.numberEnt.get())
>        tries = 1
>
>        while guess != the_number:
>            if guess > the_number:
>                      number += "Lower..."
>            else:
>                  number += "Higher..."
>            guess = int(self.numberEnt.get())
>            tries += 1
>
>            # display the text
>            self.numberTxt.delete(0.0, END)
>            self.numberTxt.insert(0.0, number)
>
>
> ----> The problem is here. You have initialised the variable "number" as a
part of the main function and trying to call it within the function of
class. You need to initialise the variable here itself, or need to pass the
variable (which you initialised in main) to this function.


> number += "You guessed it!  The number was" + the_number
> number += "And it only took you " + tries + " tries!\n"
> self.numberTxt.delete(0.0, END)
> self.numberTxt.insert(0.0, number)
>
>
>
> # main
> number = ""
> the_number = random.randint(1, 100)
> root = Tk()
> root.title("Mad Lib")
> app = Application(root)
> root.mainloop()
>
> *Output*
>
> Traceback (most recent call last):
>  File "I:\Python\programs\guess_my_ numberChapter10.py", line 60, in
> <module>
>    number += "You guessed it!  The number was" + the_number
> NameError: name 'number' is not defined
>
> Any ides??????????? Is my code going to work apart from this
> problem?????????????
>
> --
> Dave Merrick
>
> merrickdav at gmail.com
>
> Ph   03 3423 121
> Cell 027 3089 169
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <
> http://mail.python.org/pipermail/tutor/attachments/20110629/4057af9d/attachment-0001.html
> >
>

Regards,
Priyesh
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110629/4369bb28/attachment.html>


More information about the Tutor mailing list