为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

炎炎设计 2023-12-12 14:59:22
当我退出 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")#Lablesheader = 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)#Buttonsenter = Button(root, text="Enter", activebackground = "green", command=golf_records).place(x=440,y=80)root.mainloop()if __name__ == "__main__":  golf_records()    
查看完整描述

1 回答

?
慕莱坞森

TA贡献1810条经验 获得超4个赞

基本上想象一下,您的代码执行到mainloop()并在那里暂停,直到您跳出循环,即退出应用程序。然后执行后的代码mainloop(),即为true 的语句,因此运行调用激活标签的if函数。golf_record()GUI_print()

如何解决这个问题?我不确定您在这里尝试使用此代码做什么,但如果您可以将其移至语句root.mainloop()末尾if,它将在代码最初执行时执行该函数。如果您还有更多疑问,请告诉我。或者更好的方法是摆脱golf_records()内部的函数调用,if因为无论如何都有一个调用该函数的按钮。


查看完整回答
反对 回复 2023-12-12
  • 1 回答
  • 0 关注
  • 54 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信