[Tutor] Python GUI

Christopher King g.nius.ck at gmail.com
Thu Jun 30 21:37:09 CEST 2011


dude, what are all those story comments, did you just edit the mad
lib program from Python for Absolute Beginners?

On Wed, Jun 29, 2011 at 12:28 AM, 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/20110630/453250f8/attachment.html>


More information about the Tutor mailing list