Create Mad Libs Generator Game in Python

Boost Your Career with Our Placement-ready Courses – ENroll Now

It will be fun playing this game. Let’s start creating this Python Mad Libs Generator project and also learn some fun concepts while making the project.

About Mad Libs Generator

It’s a popular game among children. A story will be given to a user and without knowing the story they need to enter a word. After entering all the words, a story will be displayed to the user on the screen.

Python Mad Libs Generator Project

The purpose of this project is to develop a mad libs generator. Installation of tkinter is required to begin the project.

Project Prerequisites

Basic knowledge of functions in python and tkinter is required for this Mad Libs Generator Project.

Download Mad Libs Generator

Source code of Mad Libs Generator: Python Mad Libs Generator Project Source Code

Project File Structure

1. Install Tkinter
2. Initializing window
3. Creatine functions
4. Creating buttons

1. Install Tkinter

Tkinter is the graphical user interface toolkit designed for python. It is the fastest and the easiest way to develop graphical user interface applications.

To start the Python Mad Libs Generator project, we need to install Tkinter. To install Tkinter, run the following command on terminal or cmd.

pip install tkinter

2. Initializing window and creating buttons

Screen = Tk()
Screen.title("PythonGeeks Mad Libs Generator")
Screen.geometry('400x400')
Screen.config(bg="pink")
Label(Screen, text='PythonGeeks Mad Libs Generator').place(x=100, y=20)
#creating buttons
Story1Button = Button(Screen, text='A memorable day', font=("Times New Roman", 13),command=lambda: Story1(Screen),bg='Blue')
Story1Button.place(x=140, y=90)
Story2Button = Button(Screen, text='Ambitions', font=("Times New Roman", 13),command=lambda: Story2(Screen), bg='Blue')
Story2Button.place(x=150, y=150)
 
Screen.update()
Screen.mainloop()

Code Explanation:

Story1Button and Story2Button are the two buttons that will redirect a person to a new window where after filling the blanks, stories will be generated.

a. Tk(): It helps in displaying the window on the screen.

b. title(): It displays the title in the parentheses on the top of the window.

c. geometry(): It defines the geometry of the screen.

d. config(): It sets the background colour of the window.

e. Button(): It displays buttons on the window.

f. mainloop(): It is useful in running the event loop.

3. Creating function for first story:

def Story1(win):
  def final(tl: Toplevel, name, sports, City, playername, drinkname, snacks):
 
    text = f'''
       One day me and my friend {name} decided to play a {sports} game in {City}.
       We wanted to watch {playername}.
       We drank {drinkname} and also ate some {snacks} 
       We really enjoyed! We are looking forward to go again and enjoy '''
 
    tl.geometry(newGeometry='500x550')
 
    Label(tl, text='Story:',  wraplength=tl.winfo_width()).place(x=160, y=310)
    Label(tl, text=text,wraplength=tl.winfo_width()).place(x=0, y=330)
 
  NewScreen = Toplevel(win, bg='yellow')
  NewScreen.title("A memorable day")
  NewScreen.geometry('500x500')
  Label(NewScreen, text='Memorable Day').place(x=100, y=0)
  Label(NewScreen, text='Name:').place(x=0, y=35)
  Label(NewScreen, text='Enter a game:').place(x=0, y=70)
  Label(NewScreen, text='Enter a city:').place(x=0, y=110)
  Label(NewScreen, text='Enter the name of a player:').place(x=0, y=150)
  Label(NewScreen, text='Enter the name of a drink:').place(x=0, y=190)
  Label(NewScreen, text='Enter the name of a snack:').place(x=0, y=230)
  Name = Entry(NewScreen, width=17)
  Name.place(x=250, y=35)
  game = Entry(NewScreen, width=17)
  game.place(x=250, y=70)
  city = Entry(NewScreen, width=17)
  city.place(x=250, y=105)
  player = Entry(NewScreen, width=17)
  player.place(x=250, y=150)
  drink = Entry(NewScreen, width=17)
  drink.place(x=250, y=190)
  snack = Entry(NewScreen, width=17)
  snack.place(x=250, y=220)
  SubmitButton = Button(NewScreen, text="Submit", background="Blue", font=('Times', 12), command=lambda:final(NewScreen, Name.get(), game.get(), city.get(), player.get(), drink.get(), snack.get()))
  SubmitButton.place(x=150, y=270)
 
  NewScreen.mainloop()

Code Explanation:

Story1() function will take the desired input from the user and display it on the screen.text contains the story.

a. Label(): It prints the text in the parentheses on the window.
b. Entry(): It provides a textarea.
c. get(): It helps to get the value of the variable.
d. place(): It sets the position of a text , textarea on the screen.

4. Creating function for second story:

def Story2(win):
def final(tl: Toplevel, profession, noun, feeling, emotion,verb):
            text = f'''
            One day me and my friend {name} decided to play a {sports} game in {City}.
       But we were not able to play. So, we went to the game and watched our favourite player {playername}.
       We drank {drinkname} and also ate some {snacks} 
       We really enjoyed it! We are looking forward to go again and enjoy 
'''
 
            tl.geometry(newGeometry='500x550')
 
            Label(tl, text='Story:',  wraplength=tl.winfo_width()).place(x=160, y=310)
            Label(tl, text=text,wraplength=tl.winfo_width()).place(x=0, y=330)
    NewScreen = Toplevel(win, bg='red')
    NewScreen.title("Ambitions")
    NewScreen.geometry('500x500')
    Label(NewScreen, text='Ambitions').place(x=150, y=0)
    Label(NewScreen, text='Enter a profession:').place(x=0, y=35)
    Label(NewScreen, text='Enter a noun:').place(x=0, y=70)
    Label(NewScreen, text='Enter a feeling:').place(x=0, y=110)
    Label(NewScreen, text='Enter a emotion:').place(x=0, y=150)
    Label(NewScreen, text='Enter a verb:').place(x=0, y=190)
    Profession = Entry(NewScreen, width=17)
    Profession.place(x=250, y=35)
    Noun = Entry(NewScreen, width=17)
    Noun.place(x=250, y=70)
    Feeling = Entry(NewScreen, width=17)
    Feeling.place(x=250, y=105)
    Emotion= Entry(NewScreen, width=17)
    Emotion.place(x=250, y=150)
    Verb = Entry(NewScreen, width=17)
    Verb.place(x=250, y=190)
    SubmitButton = Button(NewScreen, text="Submit", background="Blue", font=('Times', 12), command=lambda:final(NewScreen, Profession.get(), Noun.get(), Feeling.get(), Emotion.get(), Verb.get()))
    SubmitButton.place(x=150, y=270)

Code Explanation:

Story2() does the same function as Story1(). The only difference is in the story.

Python Mad Libs Generator Output

python mad libs generator output

Summary

We have successfully created the Mad Libs Generator project, now you can create random stories. We used tkinter and functions in this project.

We work very hard to provide you quality material
Could you take 15 seconds and share your happy experience on Google | Facebook


PythonGeeks Team

At PythonGeeks, our team provides comprehensive guides on Python programming, AI, Data Science, and machine learning. We are passionate about simplifying Python for coders of all levels, offering career-focused resources.

12 Responses

  1. siddharth says:

    hey how to show the output as i dont know

  2. ESTER says:

    I LIKE IT AND ENJOY IT

  3. vani says:

    its a easy program for beginners thank u

  4. Abdullah says:

    very good project

  5. anish thakur says:

    so good project

  6. Abful says:

    I love python

  7. vaishali says:

    nil

  8. Robert Hill says:

    Very good starter project with all the building blocks. Thank you!

  9. Jacob Bridges says:

    The example file has it but the site does not.
    Add “from tkinter import *” to the top of your file if you are writing it from the site example

  10. bisha says:

    i un the code and its work and diplay the window so i wrote my story it was pretty good but im wondering as abeginer in learning python waht is the next step i supposed to follow to get the benefits from this project wether is knowlege or prctical

  11. pushkar Singh says:

    def final(tl: Toplevel, profession, noun, feeling, emotion,verb):
    ^
    IndentationError: expected an indented block after function definition on line 71

  12. Nash says:

    Great. i love it

Leave a Reply

Your email address will not be published. Required fields are marked *