首页 > 解决方案 > _tkinter.TclError:无法调用“标签”命令:应用程序已被销毁

问题描述

嗨,这是我在这个网站上的第一篇文章。最近我正在上我的编程课,我们正在学习 tkinter。我试图运行程序,盒子的东西会出现,但盒子里的文字不会出现。此错误显示“_tkinter.TclError:无法调用“标签”命令:应用程序已被破坏”。有什么问题,我该如何解决?谢谢你的帮助。

    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`

标签: pythontkinter

解决方案


您需要移动screen.mainloop()到代码的末尾:

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

看看when-do-i-need-to-call-mainloop-in-a-tkinter-application


推荐阅读