Python中tkinter的destroy和quit的区别

已收录   阅读次数: 2,203
2019-07-1017:29:36 发表评论
摘要

在python库中使用tkinter制作应用中,有一种命令是退出,对应的命令分别是destroy()以及quit(),虽然都是退出,但是有些许不同,那么有什么不同呢?从这篇文章一起来探究一下……

分享至:
Python中tkinter的destroy和quit的区别

开篇寄语

在python库中使用tkinter制作应用中,有一种命令是退出,对应的命令分别是destroy()以及quit(),虽然都是退出,但是有些许不同,那么有什么不同呢?从这篇文章一起来探究一下。

内容详情

通常,在用python搭建图形界面应用程序的时候,引用tkinter,建立一个按钮退出,一般可以这样写:

from tkinter import *

root = Tk()
button = Button(root, text="Quit", command=root.destroy)#destroy也可以换成quit
button.pack()
root.mainloop()

他们只间的区别主要是这样的:

  • quit():虽然已经退出,但是组件让人存在,一些数据还能访问
  • destroy():相当于把组件破坏了,无法再访问使用

官方文档是这样解释的:

  • quit() stops the TCL interpreter. This is in most cases what you want, because your Tkinter-app will also stop. It can be a problem, if you e.g. call your app from idle. idle is itself a Tkinker-app, so if you call quit() in your app and the TCL interpreter gets terminated, idle will also terminate (or get confused ).
  • destroy() just terminates the mainloop and deletes all widgets. So it seems to be safer if you call your app from another Tkinter app, or if you have multiple mainloops."
  • 我的微信
  • 微信扫一扫加好友
  • weinxin
  • 我的微信公众号
  • 扫描关注公众号
  • weinxin

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: