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.
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()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.