Make a GUI Magic 8 Ball App with Python and Tkinter
- Subject: Python Tutorials
- Learning Time: 30 mins
Hi Welcome to this new tutorial from MOO ICT. In this tutorial we will be making a small magic 8 ball style application using python and tkinter. This is a continuation tutorial on our Python Tutorial Series. For this application we will making this step by step in the video tutorial below. Before we start please make sure you have the PILLOW framework installed on your python. This framework allows us to use the ImageTk and Images function to load and display images to the Tkinter window.
C# Flappy Bird Hunting Game MasterClass
Create a interactive shooting game with Flappy Birds
- Use OOP to make the game
- Backgrounds and Play Music
- Work with Multiple Forms
- & Much more ..
In this new course we will recreate the very popular game with a twist. We will create this game from an empty project to a fully compiled EXE file using Visual Studio. Click here to see the full course details.
We wanted to make this app while exploring how to use images dynamically using tkinter and os framework from python. We will dynamically load the images from a specific folder because there is 15 answer images available and instead of writing and defining them one by one we can load them all to a list and use that list show an answer to the user.
Lesson Objective –
- Make a Magic 8 Ball APP using python and Tkinter
- Create functions and events and link them to the button and the entry box
- Use various colours on the GUI in tkinter
- Use OS library to loop through a folder and load the images to a list
- Use lists and random to choose a random answer for the user input
- Check user input and show error message if the entry box is empty
- Bind Click button and Enter Key Event for the application
Video Tutorial –
Source Code –
from tkinter import * from tkinter import messagebox import os import random from PIL import ImageTk, Image root = Tk() root.title("MOO ICT Magic 8 Ball App") root.geometry("610x510") # variables magic_8_ball_images =  folder_dir = "8ball_images" #loop, events and functions for images in os.listdir(folder_dir): if images.endswith(".jpg"): image = ImageTk.PhotoImage(Image.open(folder_dir+"/"+images).resize((400,350))) magic_8_ball_images.append(image) print("total imaage avilable: ", len(magic_8_ball_images)) # end of the loop # Created by MOO ICT 2022 # For educational purpose only # Adding the event def AskQuestion(event): if len(inputBox.get()) == 0: messagebox.showinfo("MOO Says: ", " Enter a Question First ") else: ShowAnswer() def ShowAnswer(): num = random.randint(0, len(magic_8_ball_images) - 1) image_label.config(image=magic_8_ball_images[num]) inputBox.delete(0, "end") #GUI components image_label = Label(root, image=magic_8_ball_images) inputBox = Entry(root, width=30, font=("Arial 18 bold"), fg="maroon", bg="yellow", justify="center") click_button = Button(root, text="Ask the Question", font=("Arial 20 bold"), bg="plum") # pack the GUI image_label.pack() inputBox.pack(pady=10) click_button.pack(side="bottom", pady=30) click_button.bind("<Button-1>", AskQuestion) inputBox.bind("<Return>", AskQuestion) root.mainloop()