can't invoke "button" command: application has been destroyed

1.9k Views Asked by At

I am first time using tkinter.Some error always show up when i execute this code. I just wanted a gui for a simple python project. errors are widgetName, self._w) + extra + self._options(cnf)) _tkinter.TclError: can't invoke "button" command: application has been destroyed

import tkinter
import os


MASTER = tkinter.Tk()
REVIEW_CONTENT_RAW = []
REVIEWS_SEL_FILM = []







def getReviewFileName(filmNum):
    basepath = os.path.abspath("..").split("/src")
    filepath = basepath[0] + "/resources/reviews"
    filename = filepath + "/review_film" + str(filmNum) +".txt";
    return filename


def loadReviewContents():
    global REVIEW_CONTENT
    for i in range(1,6):
        f = open(getReviewFileName(i), mode='r')
        fileContent = f.read().split("\n")
        REVIEW_CONTENT_RAW.append(fileContent)

def getFilmName(filmIndex):
    content = REVIEW_CONTENT_RAW[filmIndex]
    fTitle = content[0]
    return fTitle 

def getReviewListByMovie(filmIndex):
    reviewList = REVIEW_CONTENT_RAW[filmIndex]
    return reviewList

def setReviewsForSelectedFilm(filmIndex):
    global REVIEWS_SEL_FILM
    REVIEWS_SEL_FILM = getReviewListByMovie(filmIndex) 


def getReviewsForSelectedFilm():
    return REVIEWS_SEL_FILM

def populateUI():
    loadReviewContents()
    tkinter.Button(MASTER, text=getFilmName(0), 
                   command=buttonClickActionforFilm1).grid(row=1, column=0, sticky=tkinter.W, pady=4)
    tkinter.Button(MASTER, text=getFilmName(1), 
                   command=buttonClickActionforFilm2).grid(row=2, column=0, sticky=tkinter.W, pady=4)
    tkinter.Button(MASTER, text=getFilmName(2), 
                   command=buttonClickActionforFilm3).grid(row=3, column=0, sticky=tkinter.W, pady=4)
    tkinter.Button(MASTER, text=getFilmName(3), 
                   command=buttonClickActionforFilm4).grid(row=4, column=0, sticky=tkinter.W, pady=4)
    tkinter.Button(MASTER, text=getFilmName(4), 
                   command=buttonClickActionforFilm5).grid(row=5, column=0, sticky=tkinter.W, pady=4)
    tkinter.mainloop()

    return getReviewsForSelectedFilm()


def buttonClickActionforFilm1():
    global MASTER
    setReviewsForSelectedFilm(0)
    MASTER.destroy()

def buttonClickActionforFilm2():
    setReviewsForSelectedFilm(1)
    MASTER.destroy()

def buttonClickActionforFilm3():
    setReviewsForSelectedFilm(2)
    MASTER.destroy()

def buttonClickActionforFilm4():
    setReviewsForSelectedFilm(3)
    MASTER.destroy()

def buttonClickActionforFilm5():
    setReviewsForSelectedFilm(4)
    MASTER.destroy()



x = populateUI()
print(x)
1

There are 1 best solutions below

1
On

Here's your offending line(s) MASTER.destroy()

You're destroying the root Tk() window which is MASTER here hence the method name destroy(), and in turn the GUI event thread which handles the widgets, commands, etc. So, you can't invoke your buttons commands.