Python Forum
AttributeError: '_tkinter.tkapp' object has no attribute 'place_forget' - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: AttributeError: '_tkinter.tkapp' object has no attribute 'place_forget' (/thread-23255.html)



AttributeError: '_tkinter.tkapp' object has no attribute 'place_forget' - edphilpot - Dec-18-2019

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
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()



RE: AttributeError: '_tkinter.tkapp' object has no attribute 'place_forget' - michael1789 - Dec-18-2019

I did a word search on your code for "place_forget" and don't see it defined anywhere. I'm not familiar with tkinter, but if "forget()" is a function in that library then maybe your "_" should be a "."?

As it is, nothing has "place_forget" as an attribute as it doesn't exist anywhere in your code sample.


RE: AttributeError: '_tkinter.tkapp' object has no attribute 'place_forget' - woooee - Dec-19-2019

getView() returns view which is stored in another variable so view no longer exists (doesn't have place_forget or anything else). The problem here is not the error message, it is 183 lines of code that have not been tested.


RE: AttributeError: '_tkinter.tkapp' object has no attribute 'place_forget' - edphilpot - Dec-19-2019

It works in another program so it is not "untested".


RE: AttributeError: '_tkinter.tkapp' object has no attribute 'place_forget' - woooee - Dec-19-2019

So that program may or may not be tested. This program is not. Furthermore, you use globals in a class, and don't know which variables you can use. And all of the topicX functions appear to do the same thing except for the file name. One function that you pass parameters to would be better. Consider a redesign after reading up on classes.


RE: AttributeError: '_tkinter.tkapp' object has no attribute 'place_forget' - joe_momma - Dec-20-2019

edphilpot,
I created a similar script and posted it yesterday using one class my quiz_generator
in your code you have created a Frame named it view and pass that to other functions.
def getView(self, window):
        view = Frame(window)
in a class you can change it to:
def getView(self, window):
    self.view = Frame(window)
    self.view.place(x=50,y=50)
    ...
and reference it in other functions as without using global:
def unpackView(self): 
    self.view.place_forget()
    askQuestion()
best of luck,
joe