I’m taking part in Code in Place 2021 and for my final project I developed a Madlibs generator using Python and Tkinter, and the code is functional and works the way I want it to, but obviously it’s pretty long and convoluted. I was hoping some of you guys could offer some suggestions on how to make my code more concise and get rid of any unncessary lines!
I pasted all of the code below:
JavaScript
x
125
125
1
from tkinter import *
2
3
# initialize window
4
root = Tk()
5
canvas1 = Canvas(root, width=500, height=750)
6
canvas1.pack()
7
root.title('Mad Libs Generator')
8
9
# creates input fields for user to enter words
10
label1 = Label(root, text= 'Final Project Mad Libs Generator n Enter your words below!' , font = ('helvetica', 20))
11
canvas1.create_window(250, 40, window=label1)
12
13
label2 = Label(root, text='Adverb:', font = ('helvetica', 15))
14
canvas1.create_window(150, 100, window=label2)
15
16
entry1 = Entry(root)
17
canvas1.create_window(300, 100, window=entry1)
18
19
label2 = Label(root, text='Noun:', font = ('helvetica', 15))
20
canvas1.create_window(150, 130, window=label2)
21
22
entry2 = Entry(root)
23
canvas1.create_window(300, 130, window=entry2)
24
25
label2 = Label(root, text='Liquid:', font = ('helvetica', 15))
26
canvas1.create_window(150, 160, window=label2)
27
28
entry3 = Entry(root)
29
canvas1.create_window(300, 160, window=entry3)
30
31
label2 = Label(root, text='Verb:', font = ('helvetica', 15))
32
canvas1.create_window(150, 190, window=label2)
33
34
entry5 = Entry(root)
35
canvas1.create_window(300, 190, window=entry5)
36
37
label2 = Label(root, text='Number:', font = ('helvetica', 15))
38
canvas1.create_window(150, 220, window=label2)
39
40
entry6 = Entry(root)
41
canvas1.create_window(300, 220, window=entry6)
42
43
label2 = Label(root, text='Noun(Plural):', font = ('helvetica', 15))
44
canvas1.create_window(150, 250, window=label2)
45
46
entry7 = Entry(root)
47
canvas1.create_window(300, 250, window=entry7)
48
49
label2 = Label(root, text='Verb:', font = ('helvetica', 15))
50
canvas1.create_window(150, 280, window=label2)
51
52
entry8 = Entry(root)
53
canvas1.create_window(300, 280, window=entry8)
54
55
label2 = Label(root, text='Adjective:', font = ('helvetica', 15))
56
canvas1.create_window(150, 310, window=label2)
57
58
entry9 = Entry(root)
59
canvas1.create_window(300, 310, window=entry9)
60
61
label2 = Label(root, text='Noun:', font = ('helvetica', 15))
62
canvas1.create_window(150, 340, window=label2)
63
64
entry10 = Entry(root)
65
canvas1.create_window(300, 340, window=entry10)
66
67
label2 = Label(root, text='Noun(Plural):', font = ('helvetica', 15))
68
canvas1.create_window(150, 370, window=label2)
69
70
entry11 = Entry(root)
71
canvas1.create_window(300, 370, window=entry11)
72
73
label2 = Label(root, text='Illness:', font = ('helvetica', 15))
74
canvas1.create_window(150, 400, window=label2)
75
76
entry12 = Entry(root)
77
canvas1.create_window(300, 400, window=entry12)
78
79
label2 = Label(root, text='Occupation:', font = ('helvetica', 15))
80
canvas1.create_window(150, 430, window=label2)
81
82
entry14 = Entry(root)
83
canvas1.create_window(300, 430, window=entry14)
84
85
label2 = Label(root, text='Body Part(Plural):', font = ('helvetica', 15))
86
canvas1.create_window(150, 460, window=label2)
87
88
entry15 = Entry(root)
89
canvas1.create_window(300, 460, window=entry15)
90
91
label2 = Label(root, text='Body Part:', font = ('helvetica', 15))
92
canvas1.create_window(150, 490, window=label2)
93
94
entry16 = Entry(root)
95
canvas1.create_window(300, 490, window=entry16)
96
97
# function that retrieves words and transfers them into the story
98
def generateMadLib():
99
adverb=entry1.get()
100
noun=entry2.get()
101
liquid=entry3.get()
102
verb=entry5.get()
103
number=entry6.get()
104
nounp=entry7.get()
105
verb2=entry8.get()
106
adjective=entry9.get()
107
noun2=entry10.get()
108
nounp2=entry11.get()
109
illness=entry12.get()
110
occupation=entry14.get()
111
bodypartp=entry15.get()
112
bodypart=entry16.get()
113
114
label3 = Label(root, text= 'Here is your Mad Lib!', font = 'helvetica')
115
canvas1.create_window(250, 580, window=label3)
116
117
label4 = Label(root, text = ('In order to wash your face ' +adverb+ ' , you must wet your ' +noun+ ' in warm ' +liquid+'. n Then, '+verb+' it across your face '+number+' times. This will wash off any remaining '+nounp+'. n When you are done you should '+verb2+' the cloth in '+adjective+' water to clean it. nYou should also wash your face with a '+noun2+' to keep it smooth and shiny. n This will keep also keep away '+nounp2+'. Don`t worry. n It is normal to experience '+illness+' the first time you try this. n Consult your '+occupation+' if you break out in '+bodypartp+'. This works well on your '+bodypart+' too!'), font=('helvetica', 9))
118
canvas1.create_window(250, 650, window=label4)
119
120
# button that calls the function to generate the completed story
121
button1 = Button(text='Generate Lib!', command=generateMadLib, bg='red', fg='white', font=('helvetica', 9, 'bold'))
122
canvas1.create_window(250, 525, window=button1)
123
124
root.mainloop()
125
Advertisement
Answer
You can reduce your code to half if you use for
loop and list.
Here is an example, you can modify the below code according to your need:
JavaScript
1
49
49
1
from tkinter import *
2
3
# initialize window
4
root = Tk()
5
root.title('Mad Libs Generator')
6
7
# creates input fields for user to enter words
8
label1 = Label(root, text='Final Project Mad Libs Generator n Enter your words below!', font=('helvetica', 20))
9
label1.pack()
10
11
frame = Frame(root)
12
frame.pack()
13
14
lst = ['Adverb', 'Noun', 'Liquid', 'Verb', 'Number', 'Noun(Plural)',
15
'Verb', 'Adjective', 'Noun', 'Verb', 'Adjective', 'Noun(Plural)', 'Illness',
16
'Occupation', 'Body part(plural)', 'Body part']
17
18
entries = []
19
20
for row, x in enumerate(lst):
21
Label(frame, text=f'{x}:').grid(row=row, column=0)
22
ent = Entry(frame)
23
ent.grid(row=row, column=1)
24
entries.append(ent)
25
26
27
# function that retrieves words and transfers them into the story
28
def generateMadLib():
29
story = 'In order to wash your face {0}, you must wet your {1} in warm {2}.n Then, {3}'
30
'it across your face {4} times. This will wash off any remaining {5}.n When you'
31
'are done you should {6} the cloth in {7} water to clean it. nYou should also wash'
32
'your face with a {8} to keep it smooth and shiny.n This will keep also keep away'
33
'{9}. Don`t worry. n It is normal to experience {10} the first time you try this.n'
34
'Consult your {11} if you break out in {12}. '
35
'This works well on your {13} too! '.format(*(x.get() for x in entries))
36
37
label3 = Label(root, text='Here is your Mad Lib!', font='helvetica')
38
label3.pack()
39
40
label4 = Label(root, text=story)
41
label4.pack()
42
43
# button that calls the function to generate the completed story
44
button1 = Button(root, text='Generate Lib!', command=generateMadLib, bg='red', fg='white',
45
font=('helvetica', 9, 'bold'))
46
button1.pack()
47
48
root.mainloop()
49