3
\$\begingroup\$

My code creates a Mad Libs program. Its a prewritten story that you fill in blindly. There are 4 pages of 7 entry boxs and labels. When the inputs are in and next is pressed it saves inputs as variables that it puts in the story and changes the labels. When all blanks have been written in. An info box pops up with the story. Back is a wip as i can't figure out how to insert text into an entry box in a function. Is there any way I could reduce the code yet work just as efficiently? Also, is there anything I could add to make the experience more convenient like adding helpful buttons or function?

from tkinter import *
from tkinter import messagebox

root = Tk()
root.title("Mad Libs: Action Movie")

x = 0



#para is the paragraph %s is variable
Para = '''%s is a normal %s. Then, one day, a %s explodes, causing a %s to blow up, and a nearby %s erupts into a %s of flames.%s realizes that he's being chased by the government, who's trying to %s him. While on the run, he teams up with an incredibly attractive woman named %s, who has an incredible %s. She may be from the streets, but she can %s like nobody's buisness. The duo decide to turn tables on their pursuers by blowing up a %s, which triggers a chain reaction, causing the local %s, %s, and %s to explode. Then, the bad guys' helicopter gets %s by a piece of %s from when the %s exploded, and the helicopter explodes and falls onto a %s, causing it to %s, which shoots a fireball straight into the heart of %s and destroys the bad guy leader. Everything is %s and the two decide that such a %s ordeal has caused them to fall in %s with each other. They decide to celebrate by %s on the %s,and they even managed to use a %s from the beginning of the movie, to %s the whole story together.'''

l1 = Label(root, text="Man's Name")
l1.pack(side='top', fill='none', expand=False, padx=0, pady=4)#padx and pady change button location

e1 = Entry(root)
e1.pack(side='top', fill='none', expand=False, padx=1, pady=4)#labels and entry boxs

l2 = Label(root, text="Occupation")
l2.pack(side='top', fill='none', expand=False, padx=4, pady=4)

e2 = Entry(root)
e2.pack(side='top', fill='none', expand=False, padx=5, pady=4)

l3 = Label(root, text="Noun")
l3.pack(side='top', fill='none', expand=False, padx=8, pady=4)

e3 = Entry(root)
e3.pack(side='top', fill='none', expand=False, padx=9, pady=4)


l4 = Label(root, text="Noun")
l4.pack(side='top', fill='none', expand=False, padx=12, pady=4)

e4 = Entry(root)
e4.pack(side='top', fill='none', expand=False, padx=13, pady=4)

l5 = Label(root, text="Noun")
l5.pack(side='top', fill='none', expand=False, padx=16, pady=4)

e5 = Entry(root)
e5.pack(side='top', fill='none', expand=False, padx=17, pady=4)

l6 = Label(root, text="Shape")
l6.pack(side='top', fill='none', expand=False, padx=20, pady=4)

e6 = Entry(root)
e6.pack(side='top', fill='none', expand=False, padx=21, pady=4)

l7 = Label(root, text="Man's Name")
l7.pack(side='top', fill='none', expand=False, padx=24, pady=8)

e7 = Entry(root)
e7.pack(side='top', fill='none', expand=False, padx=25, pady=4)

root.geometry('500x500')


LN = Label(root, text="1 of 4")
LN.pack(side='bottom', fill='none', expand=False, padx=20, pady=4)

MN1 = ' '#defining variables
O1 = ' '
N1 = ' '
N2 = ' '
N3 = ' '
S1 = ' '
MN2 = ' '
V1 = ' '
WN1 = ' '
BP1 = ' '
V2 = ' '
N4 = ' '
N5 = ' '
RN1 = ' '
HM1 = ' '
V3 = ' '
N6 = ' '
N7 = ' '
N8 = ' '
V4 = ' '
N9 = ' '
A1 = ' '
A2 = ' '
E1 = ' '
V5 = ' '
N10 = ' '
N11 = ' '
V6 = ' '


def Next():
    global x
    global MN1
    global O1 
    global N1
    global N2
    global N3
    global S1
    global MN2
    global V1
    global WN1
    global BP1
    global V2
    global N4
    global N5
    global RN1
    global HM1
    global V3
    global N6
    global N7
    global N8
    global V4
    global N9
    global A1
    global A2
    global E1
    global V5
    global N10
    global N11
    global V6

    x = x

    if x == 0:
        l1["text"]="Verb"#7 changes text
        l2["text"]="Woman's Name"#8
        l3["text"]="Body Part"#9
        l4["text"]="Verb"#10
        l5["text"]="Noun"#11
        l6["text"]="Noun"#12
        l7["text"]="Restaurant Name"#13
        MN1 = e1.get()#saves previous variable
        O1 = e2.get()
        N1 = e3.get()
        N2 = e4.get()
        N3 = e5.get()
        S1 = e6.get()
        MN2 = e7.get()
        LN["text"]="2 of 4"
        Bck.pack(side='left')

    elif x == 1:
        l1["text"]="Historic Monument"#14
        l2["text"]="Verb Ending In ED"#15
        l3["text"]="Noun"#16
        l4["text"]="Noun"#17
        l5["text"]="Noun"#18
        l6["text"]="Verb"#19
        l7["text"]="Noun"#20
        V1 = e1.get()
        WN1 = e2.get()
        BP1 = e3.get()
        V2 = e4.get()
        N4 = e5.get()
        N5 = e6.get()
        RN1 = e7.get()
        LN["text"]="3 of 4"

    else:
        l1["text"]="Adjective"#21
        l2["text"]="Adjective"#22
        l3["text"]="Emotion"#23
        l4["text"]="Verb Ending In Ing"#24
        l5["text"]="Noun"#25
        l6["text"]="Noun"#26
        l7["text"]="Verb"#27    
        HM1 = e1.get()
        V3 = e2.get()
        N6 = e3.get()
        N7 = e4.get()
        N8 = e5.get()
        V4 = e6.get()
        N9 = e7.get()
        LN["text"]="4 of 4"
        Nxt.configure(text='Submit', command=Submit)# changes next to make it Submit when at end







    root.update()
    e1.delete(0, END)#clears input
    e2.delete(0, END)
    e3.delete(0, END)
    e4.delete(0, END)
    e5.delete(0, END)
    e6.delete(0, END)
    e7.delete(0, END)

    if x <= 3:#stops variable from increasing tooo far
        x = x+1

x = x#restates x as the value that next has increased it to

def Back():
    global x
    global MN1
    global O1 
    global N1
    global N2
    global N3
    global S1
    global MN2
    global V1
    global WN1
    global BP1
    global V2
    global N4
    global N5
    global RN1
    global HM1
    global V3
    global N6
    global N7
    global N8
    global V4
    global N9
    global A1
    global A2
    global E1
    global V5
    global N10
    global N11
    global V6

    x = x



    if x == 1:
        l1["text"]="Man's Name"#7 changes text only since variables will be changed by next
        l2["text"]="Occupation"#8
        l3["text"]="Noun"#9
        l4["text"]="Noun"#10
        l5["text"]="Noun"#11
        l6["text"]="Shape"#12
        l7["text"]="Man's Name"#13
        LN["text"]="1 of 4"
        Bck.pack_forget()

    elif x == 2:
        l1["text"]="Verb"#7
        l2["text"]="Woman's Name"#8
        l3["text"]="Body Part"#9
        l4["text"]="Verb"#10
        l5["text"]="Noun"#11
        l6["text"]="Noun"#12
        l7["text"]="Restaurant Name"#13
        LN["text"]="2 of 4"

    elif x == 3:
        l1["text"]="Historic Monument"#14
        l2["text"]="Verb Ending In ED"#15
        l3["text"]="Noun"#16
        l4["text"]="Noun"#17
        l5["text"]="Noun"#18
        l6["text"]="Verb"#19
        l7["text"]="Noun"#20
        LN["text"]="3 of 4"

    else:
        pass
    root.update()
    Nxt.configure(text='Next', command=Next)# just in case Next is Submit

    if x >= 1:# stops back from going toooo low
        x = x-1

    e1.delete(0, END)
    e2.delete(0, END)
    e3.delete(0, END)
    e4.delete(0, END)
    e5.delete(0, END)
    e6.delete(0, END)
    e7.delete(0, END)



def Submit():
    global Para
    global A1
    global A2
    global E1
    global V5
    global N10
    global N11
    global V6

    x = x
    A1 = e1.get()#gets final variables
    A2 = e2.get()
    E1 = e3.get()
    V5 = e4.get()
    N10 = e5.get()
    N11 = e6.get()
    V6 = e7.get() 
    #takes Para and fills in %s with variables chronologicaly
    messagebox.showinfo("Story", Para % (MN1, O1, N1, N2, N3, S1, MN2, V1, WN1, BP1, V2, N4, N5, RN1, HM1, V3, N6, N7, N8, V4, N9, A1, A2, E1, V5, N10, N11, V6))
    root.update()





Nxt = Button(root, text="Next", command=Next)#buttons
Nxt.pack(side='right')
Bck = Button(root, text="Back", command=Back)
Bck.pack(side='left')
Bck.pack_forget()






root.mainloop()
\$\endgroup\$
  • \$\begingroup\$ What do you mean by 'make the experience more convenient'? \$\endgroup\$ – forsvarir Jul 22 '16 at 23:02
  • \$\begingroup\$ Basically like insert previously entered text when back is hit or another button to restart or something like that. \$\endgroup\$ – PLP123 Jul 22 '16 at 23:04
  • \$\begingroup\$ This question is incomplete. To help reviewers give you better answers, please add sufficient context to your question. The more you tell us about what your code does and what the purpose of doing that is, the easier it will be for reviewers to help you. Questions should include a description of what the code does \$\endgroup\$ – Simon Forsberg Jul 23 '16 at 9:55
2
\$\begingroup\$
  1. Redundancy: the second line is completely unnecessary. Your first import already imports everything of the tkinter module, including messagebox.

  2. Clarity: I suggest to put every widget and function/method in a class like this:

    class GUI():
    
        def __init__(self, master):
    
            self.master = master # root
            self.master.title("Mad Libs: Action Movie")
    
            self.x = 0
            self.Para = '''%s is a normal %s. Then, one day, a %s explodes,
            causing a %s to blow up, and a nearby %s erupts into a %s of
            flames.%s realizes that he's being chased by the government,
            who's trying to %s him. While on the run, he teams up with an
            incredibly attractive woman named %s, who has an incredible %s.
            She may be from the streets, but she can %s like nobody's
            buisness. The duo decide to turn tables on their pursuers by
            blowing up a %s, which triggers a chain reaction, causing the
            local %s, %s, and %s to explode. Then, the bad guys' helicopter
            gets %s by a piece of %s from when the %s exploded, and the
            helicopter explodes and falls onto a %s, causing it to %s, which
            shoots a fireball straight into the heart of %s and destroys the
            bad guy leader. Everything is %s and the two decide that such a
            %s ordeal has caused them to fall in %s with each other. They
            decide to celebrate by %s on the %s,and they even managed to use
            a %s from the beginning of the movie, to %s the whole story
            together.'''
    
            self.l1 = Label(self.master, text="Man's Name")
            self.l1.pack(side='top', fill='none', expand=False, padx=0, pady=4) #padx and pady change button location
    
            self.e1 = Entry(self.master)
            self.e1.pack(side='top', fill='none', expand=False, padx=1, pady=4) #labels and entry boxs
    
            self.l2 = Label(self.master, text="Occupation")
            self.l2.pack(side='top', fill='none', expand=False, padx=4, pady=4)
    
            self.e2 = Entry(self.master)
            self.e2.pack(side='top', fill='none', expand=False, padx=5, pady=4)
    
            self.l3 = Label(self.master, text="Noun")
            self.l3.pack(side='top', fill='none', expand=False, padx=8, pady=4)
    
            self.e3 = Entry(self.master)
            self.e3.pack(side='top', fill='none', expand=False, padx=9, pady=4)
    
    
            self.l4 = Label(self.master, text="Noun")
            self.l4.pack(side='top', fill='none', expand=False, padx=12, pady=4)
    
            self.e4 = Entry(self.master)
            self.e4.pack(side='top', fill='none', expand=False, padx=13, pady=4)
    
            self.l5 = Label(self.master, text="Noun")
            self.l5.pack(side='top', fill='none', expand=False, padx=16, pady=4)
    
            self.e5 = Entry(self.master)
            self.e5.pack(side='top', fill='none', expand=False, padx=17, pady=4)
    
            self.l6 = Label(self.master, text="Shape")
            self.l6.pack(side='top', fill='none', expand=False, padx=20, pady=4)
    
            selfe6 = Entry(self.master)
            self.e6.pack(side='top', fill='none', expand=False, padx=21, pady=4)
    
            self.l7 = Label(self.master, text="Man's Name")
            self.l7.pack(side='top', fill='none', expand=False, padx=24, pady=8)
    
            self.e7 = Entry(self.master)
            self.e7.pack(side='top', fill='none', expand=False, padx=25, pady=4)
    
            self.master.geometry('500x500')
    
    
            self.LN = Label(self.master, text="1 of 4")
            self.LN.pack(side='bottom', fill='none', expand=False, padx=20, pady=4)
    
            self.MN1 = ' ' #defining variables
            self.O1 = ' '
            self.N1 = ' '
            self.N2 = ' '
            self.N3 = ' '
            self.S1 = ' '
            self.MN2 = ' '
            self.V1 = ' '
            self.WN1 = ' '
            self.BP1 = ' '
            self.V2 = ' '
            self.N4 = ' '
            self.N5 = ' '
            self.RN1 = ' '
            self.HM1 = ' '
            self.V3 = ' '
            self.N6 = ' '
            self.N7 = ' '
            self.N8 = ' '
            self.V4 = ' '
            self.N9 = ' '
            self.A1 = ' '
            self.A2 = ' '
            self.E1 = ' '
            self.V5 = ' '
            self.N10 = ' '
            self.N11 = ' '
            self.V6 = ' '
    
            self.Nxt = Button(self.master, text="Next", command=self.Next) #buttons
            self.Nxt.pack(side='right')
            self.Bck = Button(self.master, text="Back", command=self.Back)
            self.Bck.pack(side='left')
            self.Bck.pack_forget()
    
        def Next(self):
    
            if self.x == 0:
                self.l1["text"]="Verb" #7 changes text
                self.l2["text"]="Woman's Name" #8
                self.l3["text"]="Body Part" #9
                self.l4["text"]="Verb" #10
                self.l5["text"]="Noun" #11
                self.l6["text"]="Noun" #12
                self.l7["text"]="Restaurant Name" #13
                self.MN1 = self.e1.get() #saves previous variable
                self.O1 = self.e2.get()
                self.N1 = self.e3.get()
                self.N2 = self.e4.get()
                self.N3 = self.e5.get()
                self.S1 = self.e6.get()
                self.MN2 = self.e7.get()
                self.LN["text"]="2 of 4"
                self.Bck.pack(side='left')
    
            elif self.x == 1:
                self.l1["text"]="Historic Monument" #14
                self.l2["text"]="Verb Ending In ED" #15
                self.l3["text"]="Noun" #16
                self.l4["text"]="Noun" #17
                self.l5["text"]="Noun" #18
                self.l6["text"]="Verb" #19
                self.l7["text"]="Noun" #20
                self.V1 = self.e1.get()
                self.WN1 = self.e2.get()
                self.BP1 = self.e3.get()
                self.V2 = self.e4.get()
                self.N4 = self.e5.get()
                self.N5 = self.e6.get()
                self.RN1 = self.e7.get()
                self.LN["text"]="3 of 4"
    
            else:
                self.l1["text"]="Adjective" #21
                self.l2["text"]="Adjective" #22
                self.l3["text"]="Emotion" #23
                self.l4["text"]="Verb Ending In Ing" #24
                self.l5["text"]="Noun" #25
                self.l6["text"]="Noun" #26
                self.l7["text"]="Verb" #27    
                self.HM1 = self.e1.get()
                self.V3 = self.e2.get()
                self.N6 = self.e3.get()
                self.N7 = self.e4.get()
                self.N8 = self.e5.get()
                self.V4 = self.e6.get()
                self.N9 = self.e7.get()
                self.LN["text"]="4 of 4"
                self.Nxt.configure(text='Submit', command=Submit) # changes next to make it Submit when at end
    
            self.master.update()
            self.e1.delete(0, END) #clears input
            self.e2.delete(0, END)
            self.e3.delete(0, END)
            self.e4.delete(0, END)
            self.e5.delete(0, END)
            self.e6.delete(0, END)
            self.e7.delete(0, END)
    
            if self.x <= 3: #stops variable from increasing tooo far
                self.x = self.x+1 # better: self.x += 1
    
        def Back(self):
    
            if self.x == 1:
                self.l1["text"] = "Man's Name" #7 changes text only since variables will be changed by next
                self.l2["text"]="Occupation" #8
                self.l3["text"]="Noun" #9
                self.l4["text"]="Noun" #10
                self.l5["text"]="Noun" #11
                self.l6["text"]="Shape" #12
                self.l7["text"]="Man's Name" #13
                self.LN["text"]="1 of 4"
                self.Bck.pack_forget()
    
            elif self.x == 2:
                self.l1["text"]="Verb" #7
                self.l2["text"]="Woman's Name" #8
                self.l3["text"]="Body Part" #9
                self.l4["text"]="Verb" #10
                self.l5["text"]="Noun" #11
                self.l6["text"]="Noun" #12
                self.l7["text"]="Restaurant Name" #13
                self.LN["text"]="2 of 4"
    
            elif self.x == 3:
                self.l1["text"]="Historic Monument" #14
                self.l2["text"]="Verb Ending In ED" #15
                self.l3["text"]="Noun" #16
                self.l4["text"]="Noun" #17
                self.l5["text"]="Noun" #18
                self.l6["text"]="Verb" #19
                self.l7["text"]="Noun" #20
                self.LN["text"]="3 of 4"
    
            else:
                pass
            self.master.update()
            self.Nxt.configure(text='Next', command=Next) # just in case Next is Submit
    
            if self.x >= 1: # stops back from going toooo low
                self.x = self.x-1 # better: self.x -= 1
    
            self.e1.delete(0, END)
            self.e2.delete(0, END)
            self.e3.delete(0, END)
            self.e4.delete(0, END)
            self.e5.delete(0, END)
            self.e6.delete(0, END)
            self.e7.delete(0, END)
    
        def Submit(self):
    
            self.A1 = self.e1.get() #gets final variables
            self.A2 = self.e2.get()
            self.E1 = self.e3.get()
            self.V5 = self.e4.get()
            self.N10 = self.e5.get()
            self.N11 = self.e6.get()
            self.V6 = self.e7.get() 
            #takes Para and fills in %s with variables chronologicaly
            messagebox.showinfo("Story", Para % (MN1, O1, N1, N2, N3, S1, MN2, V1, WN1, BP1, V2, N4, N5, RN1, HM1, V3, N6, N7, N8, V4, N9, A1, A2, E1, V5, N10, N11, V6))
            self.master.update()
    
  3. Create a main function (like in C and C++ programs):

    def main():
    
        root = Tk()
        app = GUI(root) # app.master and root are synonyms now
        app.master.mainloop() # You also often see root.mainloop(), but in my opinion this is clearer
    
  4. Call the main function in an if-statement (that's necessary if you want to import this file in another Python script without running the main function:

    if __name__ == '__main__':
    
        main()
    
  5. Don't stop improving your code. There are probably still a lot more things to do better (such as giving your variables self-explaining names).

|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.