1

Hi this is my first post on this website. Recently I was having my programming class and we were learning about tkinter. I tried to run the program the box thing would show up but the text inside the box wouldn't show up. This error showed up "_tkinter.TclError: can't invoke "label" command: application has been destroyed". What is the issue and how can I fix it? Thanks for your help.

    from tkinter import *

    #*******************
    def greet():
        n=name.get()
        print(f"{n},good morning")

    #create a window(screen)
    screen=Tk()
    screen.title("GUI thing")
    screen.geometry("380x300")
    screen.mainloop()
    myfont="Times 14 bold"

    # Create a label and put it on the grid
    Label(screen,text="Enter Name:",font= myfont).grid(row=0,column=0)

    # Create an entry box
    name=StringVar()
    Entry(screen,width=15,font=myfont,textvariable=name).grid(row=0,column=1)

    # Create a button
    Button(screen,text="Clik moi",font=myfont,bg="green",fg="white",command=greet).grid(row=1,colum=0)`enter code here`
Bonk
  • 11
  • 1
  • 2

1 Answers1

1

You need to move screen.mainloop() to the end of your code:

from tkinter import *


# *******************
def greet():
    n = name.get()
    print(f"{n},good morning")


# create a window(screen)
screen = Tk()
screen.title("GUI thing")
screen.geometry("380x300")
myfont = "Times 14 bold"

# Create a label and put it on the grid
Label(screen, text="Enter Name:", font=myfont).grid(row=0, column=0)

# Create an entry box
name = StringVar()
Entry(screen, width=15, font=myfont, textvariable=name).grid(row=0, column=1)

# Create a button
Button(screen, text="Clik moi", font=myfont, bg="green", fg="white", command=greet).grid(row=1, column=0)

screen.mainloop()

Take a look at when-do-i-need-to-call-mainloop-in-a-tkinter-application.

kusz
  • 367
  • 1
  • 9