由于NameError,Mad Lib程序未运行?

时间:2018-06-28 16:47:53

标签: tkinter nameerror defined

我的代码根本不起作用,但是我是GUI的新手,无法识别问题。有人可以帮忙吗?

代码:

from tkinter import *

class Application(Frame):
""" GUI App to allow for the story based on user input"""

def _init_(self, master):
    """ Initialize frame."""
    Frame._init_(self, master)
    self.grid()
    self.create_widgets()

    def create_widgets(self):
        """ Create widgets to get information and display"""
        # create instruction label here
        Label(self,
              text = "It's Story Time.  Enter information for a new story"
              ).grid(row =0, column = 0, columnspan = 2, stiky = W)

        # create label and text entry for the human name
        Label(self,
              text = "Human: "
              ).grid(row = 1, column = 0, sticky = W)
        self.human_ent = Entry(self)
        self.juman_ent.grid(row = 1, column = 1, sticky = W)

        # label for plural noun
        Label(self,
              text = "Plural Noun:"
              ).grid(row = 2, column = 0, sticky = W)
        self.noun_ent = Entry(self)
        self.noun_ent.grid(row = 2, column = 1, sticky = W)

        # label for verbs
        Label(self,
              text = "Verb:"
              ).grid(row = 3, column = 0, sticky = W)
        self.verb_ent = Entry(self)
        self.verb_ent.grid(row = 2, column = 1, sticky = W)

        # label for adjective check button
        Label(self,
              text = "Adjective:"
              ).grid(row = 4, column = 0, sticky = W)

        # create bonkers button
        self.is_bonkers = BooleanVar()
        CheckButton(self,
                    text = "bonkers",
                    variable = self.is_bonk
                    ).grid(row = 4, column = 1, sticky = W)

        # create free-loading button
        self.is_free = BooleanVar()
        CheckButton(self,
                    text = "free-loading",
                    variable = self.is_free
                    ).grid(row = 4, column = 2, sticky = W)
        # create indecent button
        self.is_indecent = BooleanVar()
        CheckButton(self,
                    text = "indecent",
                    variable = self.is_indecent
                    ).grid(row = 4, column = 3, sticky = W)

        # radio buttons
        # label for body parts (I'm not creative)
        Label(self,
              text = "Body Part:"
              ).grid(row = 5, column = 0, sticky = W)

        # variables for single body part
        self.body_part = StringVar()

        # body parts radio buttons
        body_parts = ["bum", "ear lobe", "tailbone"]

        column = 1
        for part in body_parts:
            RadioButton(self,
                        text = part,
                        variable = self.body_part,
                        value = part
                        ).grid(row = 5, column = column, sticky = W)
            column += 1

            # create a submit button
            Button(self,
                   text = "Click for Awesome Story!",
                   command = self.tell_story
                   ).grid(row = 6, column = 0, sticky = W)

            self.story_txt = Test(self, width = 75, height = 10, wrap = WORD)
            self.story_txt.grid(row = 7, column = 0, columnspan = 4)


            # Application Class's tell_story() Method
            def tell_story(self):
                """ Fill text box with new story with user input. """
                # get values from the GUI
                human = self.human_ent.get()
                noun = self.noun_ent.get()
                verb = self.verb_ent.get()
                adjectives = ""
                if self.is_bonkers.get():
                    adjectives += "bonkers, "
                if self.is_free.get():
                    adjectives += "free-loading, "
                if self.is_indecent.get():
                    adjectives += "indecent, "
                body_part = self.body_part.get()

                # do hardest part (make story)
                story = "Once upon a time, there lived a mighty beast nameed "
                story += human
                story += "who lived in the dreaded Castle of "
                story += noun.title()
                story += "when, upon the Strawberry Moon, the "
                story += noun
                story += "unearthed "
                story += human + ". "
                story += "A "
                story += adjectives
                story += "thought ran through the beast's mind. "
                story += "The thought sparked something in the mind of "
                story += human
                story += "and soon the beast felt hungy for "
                story += body_part + ". "
                story += "So the beast searched for some "
                story += body_part + "flesh. "
                story += "Suddenly, the "
                story += noun
                story += "ran as quickly as possible, but it was too late.  "
                story += human + "had feasted."
                story += "The moral of the story?  Heed the warning of the Strawberry Moon, else "
                story += verb
                story += "to your doom."

                # display sad story
                self.story_txt.delete(0.0, END)
                self.story_txt.insert(0.0, story)

# Main Part of program goes here:

root = Tk()

root.title("The Strawberry Moon Mad Lib")

app = Application(root)

root.mainloop()

错误内容:

Traceback (most recent call last):
  File "C:/Users/HP/AppData/Local/Programs/Python/Python36/, Assignment 2a.py", line 4, in <module>
    class Application(Frame):
  File "C:/Users/HP/AppData/Local/Programs/Python/Python36/, Assignment 2a.py", line 148, in Application
    app = Application(root)
NameError: name 'Application' is not defined

我不知道这意味着什么,也不知道如何解决。欢迎任何帮助。

谢谢!

1 个答案:

答案 0 :(得分:0)

您的代码的总体问题是错别字和缩进。您收到的错误消息可能是由缩进引起的,但是即使更正了,由于许多错字,您仍然会在接连出错的情况下得到错误提示。

我对您的代码进行了一些整理,使其更符合PEP8标准。 您可能需要更改某些内容以提高可读性。但是格式通常是正确的。

import tkinter as tk


class Application(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)

        tk.Label(self, text="It's Story Time.  Enter information for a new story").grid(row=0, column=0, columnspan=2, sticky="w")
        tk.Label(self, text="Human: ").grid(row=1, column=0, sticky="w")
        self.human_ent = tk.Entry(self)
        self.human_ent.grid(row=1, column=1, sticky="w")

        tk.Label(self, text="Plural Noun:").grid(row=2, column=0, sticky="w")
        self.noun_ent = tk.Entry(self)
        self.noun_ent.grid(row=2, column=1, sticky="w")

        tk.Label(self, text="Verb:").grid(row=3, column=0, sticky="w")
        self.verb_ent = tk.Entry(self)
        self.verb_ent.grid(row=3, column=1, sticky="w")

        tk.Label(self, text="Adjective:").grid(row=4, column=0, sticky="w")
        self.is_bonkers = tk.BooleanVar()
        tk.Checkbutton(self, text="bonkers", variable=self.is_bonkers).grid(row=4, column=1, sticky="w")

        self.is_free = tk.BooleanVar()
        tk.Checkbutton(self, text="free-loading", variable=self.is_free).grid(row=4, column=2, sticky="w")

        self.is_indecent = tk.BooleanVar()
        tk.Checkbutton(self, text="indecent", variable=self.is_indecent).grid(row=4, column=3, sticky="w")

        tk.Label(self, text="Body Part:").grid(row=5, column=0, sticky="w")
        self.body_part = tk.StringVar()
        body_parts = ["bum", "ear lobe", "tailbone"]

        column = 1
        for part in body_parts:
            tk.Radiobutton(self, text=part, variable=self.body_part, value=part).grid(row=5, column=column, sticky="w")
            column += 1

        tk.Button(self, text="Click for Awesome Story!", command=self.tell_story).grid(row=6, column=0, sticky="w")
        self.story_txt = tk.Text(self, width=75, height=10, wrap="word")
        self.story_txt.grid(row=7, column=0, columnspan=4)

    def tell_story(self):
        human = self.human_ent.get()
        noun = self.noun_ent.get()
        verb = self.verb_ent.get()
        adjectives = ""
        if self.is_bonkers.get():
            adjectives += "bonkers, "
        if self.is_free.get():
            adjectives += "free-loading, "
        if self.is_indecent.get():
            adjectives += "indecent, "
        body_part = self.body_part.get()

        story = "Once upon a time, there lived a mighty beast named "
        story += human
        story += "who lived in the dreaded Castle of "
        story += noun.title()
        story += "when, upon the Strawberry Moon, the "
        story += noun
        story += "unearthed "
        story += human + ". "
        story += "A "
        story += adjectives
        story += "thought ran through the beast's mind. "
        story += "The thought sparked something in the mind of "
        story += human
        story += "and soon the beast felt hungry for "
        story += body_part + ". "
        story += "So the beast searched for some "
        story += body_part + "flesh. "
        story += "Suddenly, the "
        story += noun
        story += "ran as quickly as possible, but it was too late.  "
        story += human + "had feasted."
        story += "The moral of the story?  Heed the warning of the Strawberry Moon, else "
        story += verb
        story += "to your doom."

        self.story_txt.delete("1.0", "end")
        self.story_txt.insert("1.0", story)

root = tk.Tk()
root.title("The Strawberry Moon Mad Lib")
app = Application(root).grid(row=0, column=0, sticky="nsew")
root.mainloop()