【发布时间】:2018-06-28 16:47:52
【问题描述】:
我的代码根本不起作用,但我是 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
我不明白这是什么意思,也不明白该如何解决。欢迎任何帮助。
谢谢!
【问题讨论】:
-
我在运行您的代码时没有收到该错误。此外,您需要修复代码的缩进,如果您删除所有不相关的代码会有所帮助。如果问题出在应用启动中,我们真的不需要一个长篇大论的变量或一堆小部件。
-
我发现您的代码存在一些问题。
CheckButton需要是Checkbutton,RadioButton需要是Radiobutton,self.story_txt = Test()需要是self.story_txt = Text(),self.juman_ent应该是self.human_ent。您的代码中还存在一些严重的缩进问题,这将完全阻止您的代码工作。您缺少变量并且有拼写错误。 -
@Mike-SMT 我很尴尬,很快就写了这个。你能解释一下缩进问题吗?我习惯了 MATLAB。
-
@ErinWinkler 类中不完整的
init方法没有正确缩进。它必须在类下面向内 4 个空格。您在for part in body_parts:下方有缩进的行和函数,它们不应该如此。init方法需要有双下划线而不是单下划线。如果您将我的回答与您的问题进行比较,您可以查看差异。