python - _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`
解决方案
您需要移动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()
推荐阅读
- laravel - Laravel 验证唯一值
- iis-express - What does Visual Studio for Mac use to host applications?
- ruby - 使用 RSpec 和 PDK 测试内部类是否存在
- python - 在 Python 中验证文件名的格式
- java - 返回空值的递归方法
- java - 在java中将全名拆分为Salutation,Firstname和lastname
- xml - 奥多
- javascript - 如何使用 Stripe 的密钥和可发布密钥?
- c# - 如何正确序列化和反序列化 Json 中的对象列表?
- python - 使用 Python 程序将 JSON 事件发送到 Azure 事件中心