Python:删除 TKinter 框架

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3962247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 13:34:48  来源:igfitidea点击:

Python: removing a TKinter frame

pythontkinter

提问by aneuryzm

I want to remove a frame from my interface when a specific button is clicked.

我想在单击特定按钮时从我的界面中删除一个框架。

This is the invoked callback function

这是调用的回调函数

def removeMyself(self):
    del self

However, it doesn't remove itself. I'm probably just deleting the object in python without updating the interface ?

但是,它不会自行删除。我可能只是在不更新界面的情况下删除 python 中的对象?

thanks

谢谢

Update

更新

self.itemFrame = tk.Frame(parent)
self.itemFrame.pack(expand=False, side=tk.TOP)

removeB = tk.Button(self.itemFrame, text="Remove", width=10, command=self.removeIsosurface)

def removeIsosurface(self):
    self.itemFrame.Destroy()

Error message:

错误信息:

AttributeError: Frame instance has no attribute 'Destroy'

采纳答案by Steven Rumbalski

To remove, call either frm.pack_forget()or frm.grid_forget()depending on whether the frame was packed or grided.

要删除,无论是通话frm.pack_forget()还是frm.grid_forget()取决于框架是否被包装或grided。

Then call frm.destroy()if you aren't going to use it again, or hold onto the reference and repack or regrid when you want to show it again.

然后,frm.destroy()如果您不打算再次使用它,请致电,或者在您想再次展示时保留参考并重新打包或重新排列。

回答by jknair

wont this help : self.destroy()

这不会帮助: self.destroy()

chk this out : PY cookbookthe last para

chk this out : PY 食谱最后一段

回答by jknair

deldoes not delete anything.del somethingjust removes somethingfrom the local scope. And although if somethingwas the only reference to an object, it mayallow the object it to be garbage collected in the future, don't even think of using delto delete objects!!! And since selfis just a normal variables, del selfdoes nothing, except of course stopping the rest of the method from accessing the instance (so at the end of the method, it's actually like pass).

del不会删除任何内容。del something只是something从本地范围中删除。而且虽然 ifsomething是对一个对象的唯一引用,但它可能会允许该对象在未来被垃圾回收,更不要想用它del来删除对象!!!因为self它只是一个普通变量,del self所以什么都不做,当然除了停止方法的其余部分访问实例(所以在方法的末尾,它实际上就像pass)。

The exact way to remove a widget from the GUI depends on what geometry manager you use. If you used .grid(), you can use .grid_forget(). Note that this still doesn't destroy the widget - quite the contrary, you can go on and .grid()it again! - but that doesn't make any difference.

从 GUI 中删除小部件的确切方法取决于您使用的几何管理器。如果你用过.grid(),你可以用.grid_forget()。请注意,这仍然不会破坏小部件 - 恰恰相反,您可以再继续下去.grid()!- 但这没有任何区别。

回答by Rafe Kettler

Let's say you're making a class. You have to do a couple of things special here:

假设您正在上课。你必须在这里做一些特别的事情:

  • The frame you want to destroy has to be an instance variable
  • You have to write a callback (which you did)
  • 您要销毁的框架必须是一个实例变量
  • 你必须写一个回调(你做了)

So, here's how a basic prototype would look.

所以,这是一个基本原型的样子。

from Tkinter import Tk, Frame, Button, Label

class GUI:

    def __init__(self, root):
        self.root = root # root is a passed Tk object
        self.button = Button(self.root, text="Push me", command=self.removethis)
        self.button.pack()
        self.frame = Frame(self.root)
        self.frame.pack()
        self.label = Label(self.frame, text="I'll be destroyed soon!")
        self.label.pack()

    def removethis(self):
        self.frame.destroy()

root = Tk()
window = GUI(root)
root.mainloop()

Happy hunting!

狩猎快乐!