0

Hi everyone

I have this code that I am using from a book, it seems that it does not run correctly, I am new to tkinter and trying to understand how its lay out is supposed to work, is this code up-to-date with using pycharm?
I have tried pycharm and IDLE, but it does not display all the widgets as talked about in the book, is there other modules/functions that should be arranged into this or have I done too many typos?

            from tkinter import *

            class Application(Frame):
                def __init__(selfself, master):
                    super(Application, self).__init__(master)
                    self.grid()
                    self.create_widgets()

                def create_widgets(self):
                    Label(self, text="enter information for story").grid(row=0, column=0, columnspan=2, sticky=W)
                    Label(self, text="person:").grid(row=1, column=0, sticky=W)
                    self.person_ent=Entry(self)
                    self.person_ent.grid(row=1, column=1, sticky=W)
                    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(self, text="verb:").grid(row=3, column=0, sticky=W)
                    self.verb_ent = Entry(self)
                    self.verb_ent.grid(row=3, column=1, sticky=W)
                    Label(self, text="Adjectives(S)").grid(row=4, column=0, sticky=W)
                    self.is_itchy = BooleanVar()
                    Checkbutton(self, text="joyous", variable=self.is_joyous).grid(row=4, column=2, sticky=W)
                    self.is_electric=BooleanVar()
                    Checkbutton(self, text="electric", variable=self.is_electric).grid(row=4, column=3, sticky=W)
                    Label(self, text="Body Part:").grid(row=5, column=0, sticky=W)
                    self.body_part = StringVar()
                    self.body_part.set(NONE)
                    BODY_PARTS = ["bellybutton", "big toe", "medulla oblongata"]
                    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

                    Button(self, text="click for story", command=self.tell_story).grid(row=6, column=0, sticky=W)
                    self.story_txt = Text(self, width=75, height=75, wrap=WORD)
                    self.story_txt.grid(row=7, column=0, columnspan=4)
                def tell_story(self):
                    person = self.person_ent.get()
                    noun = self.noun_ent.get()
                    verb = self.verb_ent.get()
                    adjectives = ""
                    if self.is_itchy.get():
                        adjectives += "itchy,"
                    if self.is_joyous.get():
                        adjectives += "joyous,"
                    if self.is_electric.get():
                        adjectives += "electric"
                    body_part = self.body_part.get()

                    story = "the famous explorer"
                    story += person
                    story += "had nearly given up a life long quest to find the lost city"
                    story += noun.title()
                    story += "when one day, the"
                    story += noun
                    story += "found"
                    stroy += person + "."
                    story += " a strong, "
                    story += adjectives
                    story += "peculiar feeling overwhelmed the explorer"
                    story += "after all this time the quest was finally over . a tear came down"
                    story += person + "'s"
                    story += body_part + "."
                    story += "and then, the"
                    story += noun
                    story += "promptly devoured"
                    stroy += person + "."
                    story += "the moral of the story? be careful what you do"
                    story += verb
                    story += "for"

            self.story_txt.delete(0.0, END)
            self.story_txt.insert(0.0, story)

            root = Tk()
            root.title("stroy tale")
            app = Application(root)
            root.mainloop()
1
Contributor
0
Replies
6
Views
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.