Hi, I've got an issue with my quiz game, when the question is answered, the screen is meant to be cleared so that a new question and answers can be displayed, however I get the error message:
Exception in Tkinter callback
Exception in Tkinter callback
Error:Traceback (most recent call last):
File "C:\Users\ed\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:\Users\ed\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 749, in callit
func(*args)
File "C:\Users\ed\Documents\Revision Game\Game.py", line 24, in <lambda>
view.after(1000, lambda *args: self.unpackView(view))
File "C:\Users\ed\Documents\Revision Game\Game.py", line 51, in unpackView
view.place_forget()
File "C:\Users\ed\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 2101, in __getattr__
return getattr(self.tk, attr)
AttributeError: '_tkinter.tkapp' object has no attribute 'place_forget'my code is as follows:import random
from tkinter import *
from time import sleep
right = 1
correct = 0
questions = []
index = -1
number_of_questions = len(questions)
#=========================================Frames============================================
class Question:
def __init__(self, question, answers, correctLetter):
self.question = question
self.answers = answers
self.correctLetter = correctLetter
def check(self, letter, view):
global correct
if(letter == self.correctLetter):
label = Label(view, text="Right!")
correct = correct + 1
else:
label = Label(view, text="Wrong!")
label.pack()
view.after(1000, lambda *args: self.unpackView(view))
def getView(self, window):
view = Frame(window)
label = Label(window, font=('arial', 14, 'bold'),bg='gray',fg='white',bd=5,width=60,
justify=CENTER,text=self.question)
button_a = Button(window, font=('arial', 14, 'bold'),bg='red',fg='white',bd=1,width=23, height=2,
justify=CENTER,text=self.answers[0],command=lambda *args: self.check("A", window))
button_b= Button(window, font=('arial', 14, 'bold'), bg='red', fg='white', bd=1, width=23,height=2,
justify=CENTER,text=self.answers[1],command=lambda *args: self.check("B", window))
button_c= Button(window, font=('arial', 14, 'bold'), bg='red', fg='white', bd=1, width=23,height=2,
justify=CENTER,text=self.answers[2],command=lambda *args: self.check("C", window))
button_d= Button(window, font=('arial', 14, 'bold'), bg='red', fg='white', bd=1, width=23,height=2,
justify=CENTER,text=self.answers[3],command=lambda *args: self.check("D", window))
label.place(x=400, y=550)
button_a.place(x=450, y=625)
button_b.place(x=800, y=625)
button_c.place(x=450, y=700)
button_d.place(x=800, y=700)
return view
def unpackView(self, view):
view.place_forget()
askQuestion()
def askQuestion():
global questions, window, index, button, right, number_of_questions
if(len(questions) == index + 1):
Label(text="Thank you for answering the questions. " + str(correct) + " of 5 questions answered correct").place(x=500, y=400)
return
button1.place_forget()
button2.place_forget()
button3.place_forget()
button4.place_forget()
button5.place_forget()
index += 1
questions[index].getView(window).place(x=50,y=50)
#=======================================Questions=============================================
def topic1():
global questions
file = open("Systems_Architecture.txt", "r")
line = file.readline()
while(line != ""):
questionString = line
answers = []
for i in range (4):
answers.append(file.readline())
correctLetter = file.readline()
correctLetter = correctLetter[:-1]
questions.append(Question(questionString, answers, correctLetter))
line = file.readline()
index = -1
right = 0
number_of_questions = len(questions)
askQuestion()
file.close()
def topic2():
global questions
file = open("Network_Topologies.txt", "r")
line = file.readline()
while(line != ""):
questionString = line
answers = []
for i in range (4):
answers.append(file.readline())
correctLetter = file.readline()
correctLetter = correctLetter[:-1]
questions.append(Question(questionString, answers, correctLetter))
line = file.readline()
index = -1
right = 0
number_of_questions = len(questions)
askQuestion()
file.close()
def topic3():
global questions
file = open("Datatypes.txt", "r")
line = file.readline()
while(line != ""):
questionString = line
answers = []
for i in range (4):
answers.append(file.readline())
correctLetter = file.readline()
correctLetter = correctLetter[:-1]
questions.append(Question(questionString, answers, correctLetter))
line = file.readline()
index = -1
right = 0
number_of_questions = len(questions)
askQuestion()
file.close()
def topic4():
global questions
file = open("Programming.txt", "r")
line = file.readline()
while(line != ""):
questionString = line
answers = []
for i in range (4):
answers.append(file.readline())
correctLetter = file.readline()
correctLetter = correctLetter[:-1]
questions.append(Question(questionString, answers, correctLetter))
line = file.readline()
index = -1
right = 0
number_of_questions = len(questions)
askQuestion()
file.close()
def topic5():
global questions
file = open("All.txt", "r")
line = file.readline()
while(line != ""):
questionString = line
answers = []
for i in range (4):
answers.append(file.readline())
correctLetter = file.readline()
correctLetter = correctLetter[:-1]
questions.append(Question(questionString, answers, correctLetter))
line = file.readline()
index = -1
right = 0
number_of_questions = len(questions)
askQuestion()
file.close()
def closeFrame():
window.destroy()
window = Tk()
window.attributes('-fullscreen', True)
render = PhotoImage(file="Background.gif")
img= Label(window, image = render)
img.place(x=0,y=0)
button1 = Button(window, text="Systems Architecture", command=topic1)
button2 = Button(window, text="Network Topologies", command=topic2)
button3 = Button(window, text="Data Types", command=topic3)
button4 = Button(window, text="Programming", command=topic4)
button5 = Button(window, text="All Questions", command=topic5)
buttonClose = Button(window,font=('arial', 14, 'bold'), bg='red', fg='white', bd=1, width=2,height=1,text = "X", command = closeFrame)
button1.place(x=700, y=500)
button2.place(x=700, y=530)
button3.place(x=700, y=560)
button4.place(x=700, y=590)
button5.place(x=700, y=620)
buttonClose.place(x=1500,y=10)
window.mainloop()
