Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help ASAP problem: Make a GUI to replicate the magic 8 ball. Your GUI shoul

ID: 3718823 • Letter: N

Question

Need help ASAP
problem: Make a GUI to replicate the magic 8 ball. Your GUI should use the Canvas widget on Python to create an image of the magic 8 ball (hint: the 8 can be created with 2 ovals.) At minimum your GUI should contain a label to prompt the user to type their question, and entry to allow the user to type their question, a button that will generate a response, a label in which the message will appear, and a button to quit. You should create a hardcoded list with 14 possible answers rather than asking the user to enter the possible answers in a loop, however, the printed answers should still be randomly generated. The window should disappear when the quit button is clicked.

Page Problem: Expand on the problem from Lab 7 (Magic 8 ball) by making a GUI to replicate the Magic 8 ball. Your GUI should use the Canvas widget to create an image of the Magic 8 ball similar to that shown below (hint the 8 was created using two ovals). At a minimum your GUI should contain a label to prompt the user to enter the question, an entry to allow the user to type their question, a button that will generate a response, a label in which the message will appear, and a button to quit. You also may add other widgets. The words "message 1" should be replaced with one of the answers given Lab 7 when the "Click me for answer" button is clicked. You should create a hardcoded list with the fourteen (14) possible answers rather than asking the user to enter the possible answers in a loop, however, the printed answer should still be randomly generated as in Lab 7. The window should disappear when the quit button is clicked Although your finished code must have the widgets shown below and described above are welcome to play with colors and additional widgets, ie. have some fun! then slici sn buttur

Explanation / Answer

CODING:

import tkinter as tk

import random as rn

class BallData:

# A class object to represent the logic of the program

# I don't like globals

  

# Put all the answers in one variable [in a tuple]

answers = ["Yes, Most Definately!",

"The Chances Are High!",

"Not Likely!",

"The Odds Look Good!",

"Is That Your Question?",

"You Have To Wait And See!",

"23% Chance!",

"99.9% Success Rate!",

"It looks good!",

"Computer Says NO!",

"Unable to Decide!"

"Ask Again Later!",

"Better Not Tell You Now!",

"Cannot Predict Result!",

"Concentrate & Ask Again!",

"Dont Count On It!",

"No Chance!",

"Only Smarties Have The Answer!"]

  

error_msg = "ERROR: Your question must be TEXT"

  

# takes a string (userentry) and returns an answer

def get_respose(self,userentry):

y = userentry.isdigit()

l = len(userentry)

if y or l == 0:

return self.error_msg

else:

return rn.choice(self.answers)

class View(tk.Frame):

# This window contains the user interface

def __init__(self,master,*args,**kwargs):

tk.Frame.__init__(self,master,*args,**kwargs)

  

self.data = BallData()

  

self.configure(bg='Black')

  

self.user_question = tk.StringVar()

self.reply = tk.StringVar()

  

# Make the buttons and entry boxes (but not packing them just yet)

# These are the labels (to display text)

lbl1 = tk.Label(self, text='Enter Your Question', bg='Black', fg='Red', font='freesansbold')

lbl2 = tk.Label(self, text='Answer', bg='Black', fg='Red', font='freesansbold')

lbl3 = tk.Label(self, text='0 - 0', bg='Black', font='Black')

self.lbl_reply = tk.Label(self,font='freesansbold',textvariable=self.reply)

# These are the two buttons (quit and get-answer)

btn1 = tk.Button(self, text='Get Answer', bg='darkBlue', fg='Red', font='freesansbold', command=self.get_answer)

btn2 = tk.Button(self, text='Exit', bg='Red', fg='Black', font='freesansbold', command=self.quit)

btn3 = tk.Button(self, text='Clear', bg='Green', fg='Black', font='freesansbold', command=self.clear)

# entry field

self.user_entry = tk.Entry(self, bg='Black', fg='Red', font='freesansbold, 12',textvariable = self.user_question)

  

# Pack everything into the window (in order)

lbl1.pack()

self.user_entry.pack()

btn1.pack()

lbl3.pack()

lbl2.pack()

self.lbl_reply.pack()

btn3.pack()

btn2.pack(side=tk.BOTTOM)

  

def get_answer(self):

self.reply.set(self.data.get_respose(self.user_question.get()))

  

def clear(self):

self.user_question.set("")

self.reply.set("")

  

class MainWindow(tk.Tk):

# The main window has the title bar and could have a menu

def __init__(self):

tk.Tk.__init__(self)

  

self.minsize(300,300)

self.title('Magic 8 Ball - Ben Woodfield')

View(self).pack()

# create a main window and call mainloop

MainWindow().mainloop()

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote