Question
This is my questions I am getting this message Exception in Tkinter callback TypeError: 'str' object is not callable How do I fix this This
This is my questions
I am getting this message Exception in Tkinter callback
TypeError: 'str' object is not callable
How do I fix this
This is the code
# Import the required modules.
import tkinter
import tkinter.messagebox
import json
import random
class ProgramGUI:
def __init__(self):
# This is the constructor of the class.
# It is responsible for loading and reading the data from the text file and creating the user interface.
# See the "Constructor of the GUI Class of quiz.py" section of the assignment brief.
# Create the main window,
self.main_window = tkinter.Tk()
# set title to "Quiz"
self.main_window.title('Quiz')
# set default window size to width = 300 and hieght = 100
self.main_window.geometry('800x150')
self.main_window.configure(bg='skyBlue2', cursor='arrow')
self.main_window.resizable(width=False, height=False)
try:
# Open the file in read mode
f = open('data.txt', 'r')
# Read the json data into the 'data' variable
self.data = json.load(f)
f.close()
if len(self.data) < 5:
# Display the error message
tkinter.messagebox.showerror('Error','Insufficient Number of Questions')
# Destroy the main program
self.main_window.destroy()
return
except FileNotFoundError:
# Display the error message
tkinter.messagebox.showerror('Error','Missing/Invalid file')
# Destroy the main program
self.main_window.destroy()
return
except:
# Display the error message
tkinter.messagebox.showerror('Error','An Error has Occured')
# Destroy the main program
self.main_window.destroy()
return
self.question = random.sample(self.data, 5)
self.correct_answer = 0
self.current_question = 0
self.completed_questions = 0
self.score = 0
#GUI PROGRAM
#Create 3frames to gourp widgets
self.top_frame = tkinter.Frame(self.main_window)
self.middle_question_frame = tkinter.Frame(self.main_window)
self.middle_answer_frame = tkinter.Frame(self.main_window)
self.bottom_difficulty_frame = tkinter.Frame(self.main_window)
self.bottom_score_frame = tkinter.Frame(self.main_window)
#Create 3 frames to gourp widgets
self.top_frame.pack()
self.middle_question_frame.pack()
self.middle_answer_frame.pack()
self.bottom_difficulty_frame.pack()
self.bottom_score_frame.pack()
self.top_label= tkinter.Label(self.top_frame)
self.top_label.pack(side='top')
self.middle_question_label= tkinter.Label(self.middle_question_frame)
self.middle_question_label.pack(side='top')
self.middle_answer_entry= tkinter.Entry(self.middle_answer_frame, width = 20)
self.middle_answer_button= tkinter.Button(self.middle_answer_frame, text = "Submit Answer",bg='skyBlue2',command = lambda:self.check_answer())
self.middle_answer_entry.pack(side='left')
self.middle_answer_button.pack(side='left')
self.bottom_difficulty_label= tkinter.Label(self.bottom_difficulty_frame)
self.bottom_difficulty_label.pack(side='top')
self.bottom_score_label= tkinter.Label(self.bottom_score_frame)
self.bottom_score_label.pack(side='top')
self.show_question()
tkinter.mainloop()
def show_question(self):
# This method is responsible for displaying the current question and some other messages in the GUI.
self.middle_answer_entry.delete(0,'end')
self.current_question =self.current_question + 1
self.show_question =self.question[self.current_question]['Question']
self.top_label.configure(text = 'Question {}/{}'.format(self.current_question,5),bg='skyBlue2')
# if difficulity is greater or equal to 4 show a message
if self.question[self.current_question]['Difficulty'] >= 4:
self.bottom_difficulty_label.configure(text= 'This is a hard one',bg='skyBlue2')
else:
self.bottom_difficulty_label.pack_forget()
#Display Question
self.middle_question_label.configure(text = self.show_question, bg='skyBlue2')
self.bottom_score_label.configure(text = 'Good luck!', bg='skyBlue2')
self.completed_questions = self.completed_questions + 1
#Set Textbook to enter answers
text_entry =self.middle_answer_entry.focus()
return text_entry
def check_answer(self):
# This method is responsible for checking if the user's answer is correct when the button is clicked.
check_answer = self.middle_answer_entry.get().strip()
if len(check_answer) == 0:
# Display the error message
tkinter.messagebox.showerror('Error= invalid Input','Please enter an answer')
return check_answer
if check_answer in self.question[self.current_question]['Answer']:
self.correct_answer = self.correct_answer + 1
self.score = self.score + (self.question[self.current_question]['Difficulty']*2)
tkinter.messagebox.showinfo('Correct','Good job- you got it right!')
else:
tkinter.messagebox.showinfo('Incorrect',"The anwer is Incorrect")
if self.completed_questions == 5:
tkinter.messagebox.showinfo('Final Score','Game Over Final Score : {} Thank you'.format(self.score))
self.main_window.destroy()
else:
self.show_question()
gui = ProgramGUI()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started