Dtal
Dtal

Reputation: 23

Python: tkinter.TclError: can't invoke "label" command: application has been destroyed

When i exit the tk window i get this long library error, and I have no idea how to fix it. the GUI itself and error message are below the code.

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()    

enter image description here

enter image description here

Upvotes: 1

Views: 723

Answers (1)

Delrius Euphoria
Delrius Euphoria

Reputation: 15088

Basically imagine like your code is executed till the mainloop() and it pauses there till you break out of the loop, ie, by exiting the application. Then the code after the mainloop() gets executed, ie, the if statements which is true, hence running the function golf_record() which calls the GUI_print() which activates the label.

How to solve this? Im not sure what your trying to do with this code here, but if you can move that root.mainloop() to the end of the if statement, itll execute the function while the code is initially executed. Do let me know if you have more doubts. Or the better way would be to get rid of the function call golf_records() inside the if because you have a button that calls the function anyway.

Upvotes: 2

Related Questions