patternpythonMinor
Mad Libs program
Viewed 0 times
madprogramlibs
Problem
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 box
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 box
Solution
-
Redundancy: the second line is completely unnecessary. Your first
-
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()
Redundancy: the second line is completely unnecessary. Your first
import already imports everything of the tkinter module, including messagebox.-
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()
Code Snippets
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)
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 clearerif __name__ == '__main__':
main()Context
StackExchange Code Review Q#135645, answer score: 2
Revisions (0)
No revisions yet.