Python:tkinter.TclError:无法调用“标签”命令:应用程序已被破坏

当我退出 tk 窗口时,我收到这个长库错误,而且我不知道如何修复它。GUI 本身和错误消息位于代码下方。


def golf_records()->None:

  """

  This function prints the information

  of golfer names and the score they have 

  """

  with open("golf.txt", "r") as myfile:

    myfile_content = myfile.read()

    GUI_print(myfile_content)


def GUI_print(data)->None:

  """

  The function takes in a string found in golf.txt file

  and prints it out through tkinter GUI

  """

  my_label = Label(root,text = str(data),font=("ariel",15)).place(x=250,y=120)

  


root = Tk()

root.geometry("600x600")


#Lables

header = Label(root,text = "Current Golf Records",font=("ariel",15,"bold")).place(x=150,y=20)

header = Label(root,text = "-----------------------------",font=("ariel",15)).place(x=150,y=50)

header = Label(root,text = "Press enter to list players data: ",font=("ariel",15)).place(x=150,y=80)


#Buttons

enter = Button(root, text="Enter", activebackground = "green", command=golf_records).place(x=440,y=80)



root.mainloop()


if __name__ == "__main__":

  golf_records()    

https://img1.sycdn.imooc.com/657805250001cdd706000403.jpg

https://img1.sycdn.imooc.com/6578053000014a7c06520190.jpg

炎炎设计
浏览 62回答 1
1回答

慕莱坞森

基本上想象一下,您的代码执行到mainloop()并在那里暂停,直到您跳出循环,即退出应用程序。然后执行后的代码mainloop(),即为true 的语句,因此运行调用激活标签的if函数。golf_record()GUI_print()如何解决这个问题?我不确定您在这里尝试使用此代码做什么,但如果您可以将其移至语句root.mainloop()末尾if,它将在代码最初执行时执行该函数。如果您还有更多疑问,请告诉我。或者更好的方法是摆脱golf_records()内部的函数调用,if因为无论如何都有一个调用该函数的按钮。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python