不能调用"按钮"命令:应用程序已被破坏

时间:2016-04-11 17:55:34

标签: python user-interface tkinter pygui

我第一次使用tkinter。当我执行此代码时,总会出现一些错误。我只想要一个简单的python项目的gui。错误是 widgetName,self._w)+ extra + self._options(cnf)) _tkinter.TclError:无法调用"按钮"命令:应用程序已被销毁

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 个答案:

答案 0 :(得分:1)

这是您的违规行MASTER.destroy()

您在此处销毁Tk()MASTER窗口,因此方法名称destroy(),以及处理窗口小部件,命令等的GUI事件线程那么,你不能调用你的按钮命令。