I am using PYTHON to type the code. this is the file containing the responses: Y
ID: 3713109 • Letter: I
Question
I am using PYTHON to type the code.
this is the file containing the responses:
Yes, of course!
Without a doubt, yes.
You can count on it.
For sure!
Ask me later.
I’m not sure.
I can’t tell you right now.
I’ll tell you after my nap.
No way!
I don’t think so.
Without a doubt, no.
The answer is clearly NO
Problem: Expand on the second problem from Project 3 (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 in the file from Project 4. You may create a hardcoded list with the possible answers rather than reading the possible answers from a file, however, the answer should still be randomly printed. You are welcome to play with colors and different widgets, i.e. have some fun! tk Ask question, then click on button Click me for answer message 1 QuitExplanation / Answer
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.