[Tutor] Python GUI

Luke Paireepinart rabidpoobear at gmail.com
Wed Jun 29 06:52:15 CEST 2011


The error message tells you everything... On line 60, you try to add to "number" but the variable hasn't been defined.

We aren't going to debug your code for you, do YOU think it will work apart from this? Have you tried running any of the code? It's almost always better to build small parts and test along the way than to write out a whole program and try it at the end.

-----------------------------
Sent from a mobile device. Apologies for brevity and top-posting.
-----------------------------

On Jun 28, 2011, at 11:28 PM, David Merrick <merrickdav at gmail.com> wrote:

> # 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)
>             
> 
> 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
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110628/3510c3ac/attachment-0001.html>


More information about the Tutor mailing list