Python Forum
AttributeError: '_tkinter.tkapp' object has no attribute 'place_forget'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
AttributeError: '_tkinter.tkapp' object has no attribute 'place_forget'
#1
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()
Reply
#2
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.
Reply
#3
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.
Reply
#4
It works in another program so it is not "untested".
Reply
#5
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.
Reply
#6
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] AttributeError: '' object has no attribute 'tk' Maryan 2 1,215 Oct-29-2020, 11:57 PM
Last Post: Maryan
  [Tkinter] AttributeError: 'tuple' object has no attribute 'replace' linuxhacker 7 882 Aug-08-2020, 12:47 AM
Last Post: linuxhacker
  [Tkinter] _tkinter.TclError: bitmap "Icon.gif" not defined djwilson0495 2 1,241 Aug-05-2020, 02:27 PM
Last Post: wuf
  [Tkinter] _tkinter.TclError: image "pyimage2" doesn't exist Killdoz 1 1,793 May-30-2020, 09:48 AM
Last Post: menator01
  [Kivy] AttributeError: 'NoneType' object has no attribute 'bind' faszination_92 2 1,382 Apr-12-2020, 07:01 PM
Last Post: Larz60+
  "ModuleNotFoundError: No module named '_tkinter' in Python 3.8 Alfa 0 programmerc 1 3,311 Oct-21-2018, 06:32 PM
Last Post: Larz60+
  [Tkinter] AttributeError: 'App' object has no attribute 'set_text' Sahil1313 6 6,247 Jun-17-2018, 05:01 AM
Last Post: woooee
  'list' object has no attribute 'plot' problem BaartCM 2 8,677 Feb-03-2018, 09:20 PM
Last Post: BaartCM
  [Tkinter] How to find what causing AttributeError: '_tkinter.tkapp' ? darktitan 8 21,805 Sep-18-2016, 11:06 AM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020