Write an object-oriented Python program that will allow the teacher to enter thr
ID: 3865865 • Letter: W
Question
Write an object-oriented Python program that will allow the teacher to enter three test scores, then calculate and display their average rounded to one decimal place.
Use python 3
Write the program using tkinter to produce the GUI interface below.
Create a calc_avg() method to calculate the average of the three test scores.
The output from your program should look like this …
Once you have your program running as shown above, try clicking the Average button before entering any test scores. Also, try entering non-numeric data. Write the try/except block to handle the exception that you see. DO NOT WRITE AN IF STATEMENT. If the exception is thrown, the exception handler should use an info dialog box widget to display the error message to the user, as shown below …
te st1 frame Enter the score for test 1 Enter the score for test 2: Enter the score for test 3 test2_frame test3_frame avg frame Average Quit button_frameExplanation / Answer
Here is the code segment of the given criteria, please go through it:-
CALC_GUI.py
import tkinter
class CALC:
def __init__(self):
#create the main window
self.main_window = tkinter.Tk()
#Create 5 frames to be packed on top of each other in the window
self.test1_frame = tkinter.Frame(self.main_window)
self.test2_frame = tkinter.Frame(self.main_window)
self.test3_frame = tkinter.Frame(self.main_window)
self.avg_frame = tkinter.Frame(self.main_window)
self.button_frame = tkinter.Frame(self.main_window)
#Create and pack the label and entry widgets for test1
self.test1_label = tkinter.Label(self.test1_frame, text = "Enter the score for test1:")
self.test1_entry = tkinter.Entry(self.test1_frame, width=10)
self.test1_label.pack(side="left")
self.test1_entry.pack(side="left")
#Create and pack the label and entry widgets for test2
self.test2_label = tkinter.Label(self.test2_frame, text = "Enter the score for test2:")
self.test2_entry = tkinter.Entry(self.test2_frame, width=10)
self.test2_label.pack(side="left")
self.test2_entry.pack(side="left")
#Create and pack the label and entry widgets for test3
self.test3_label = tkinter.Label(self.test3_frame, text = "Enter the score for test3:")
self.test3_entry = tkinter.Entry(self.test3_frame, width=10)
self.test3_label.pack(side="left")
self.test3_entry.pack(side="left")
#Create and pack the widgets for average
self.result_label = tkinter.Label(self.avg_frame, text='CALC:')
self.avg = tkinter.StringVar()
self.avg_label = tkinter.Label(self.avg_frame, textvariable = self.avg)
self.result_label.pack(side='left')
self.avg_label.pack(side='left')
#Create and pack the button widgets.
self.calc_button = tkinter.Button(self.button_frame, text='Calculate', command=self.calc_CALC)
self.quit_button = tkinter.Button(self.button_frame, text='Quit', command=self.main_window.destroy)
self.calc_button.pack(side='left')
self.quit_button.pack(side='left')
#Pack the frames
self.test1_frame.pack()
self.test2_frame.pack()
self.test3_frame.pack()
self.avg_frame.pack()
self.button_frame.pack()
#Start the main loop
tkinter.mainloop()
#The calc_CALC method is the callback function for the calc_button widget.
def calc_CALC(self):
# get the score of test1, test2 and test3 and store them in varialbes
self.test1 = float(self.test1_entry.get())
self.test2 = float(self.test2_entry.get())
self.test3 = float(self.test3_entry.get())
#Caculate the CALC
self.CALC = self.test1+self.test2+self.test3/3
#update the avg_label widget by storing the value of self.CALC in the StringVar
#object referenced by average.
self.avg.set(self.CALC)
#Create an instance of the CALC class
calc_CALC = CALC()
============================================
Please let me know if you have any concern.
Thankyou
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.