销毁动态创建的小部件 [英] Destroying a dynamically created widget

查看:50
本文介绍了销毁动态创建的小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本类似的问题,但我觉得没有正确回答:

I have basically a similar question, though I do not feel it has been answered properly:

Tkinter:如何动态创建一个可以被销毁或删除的小部件?

接受的答案是:

您需要将动态创建的小部件存储在列表中.有类似的东西

You'll want to store the dynamically-created widgets in a list. Have something like

     dynamic_buttons = []

     def onDoubleClick(event):
     ...
     button = Button(...)
     dynamic_buttons.append(button)
     button.pack() 

You can then access the buttons for removal with, say,

     dynamic_buttons[0].destroy()

你可以看到他们所说的引用是不可变的,这里使用了数字 0.但是在动态创建小部件时,如何将这些引用连接到按钮?

You can see that the reference they speak of is not variable, here the number 0 is used. But when dynamically creating widgets, how do you connect these references to the buttons?

假设您创建了一个 Toplevel 小部件(显示文件的内容),并希望有一个按钮来关闭该小部件.动态创建将允许打开多个文件.问题是,即使有这个列表,按钮如何知道"它属于哪个小部件,因为没有硬参考(很好,你有一个项目列表,但顶层 5 + 按钮 5 不知道它们是在他们的名单中排名第 5).按钮和顶层总是只有一个活动"版本,这个可以删除.

Say that you create a Toplevel widget (displays a file's content), and want to have a button to close the widget. The dynamic creation will allow multiple files to be open. The problem is that even with this list, how will the button "know" to which widget it belongs, as there is no hard reference (great that you have a list of the items, but toplevel 5 + button 5 have no clue they are 5th in their lists). There will always be just one "active" version of the button and the toplevel, and this one can be deleted.

aanstuur_files = []
aanstuur_frames = []
aanstuur_buttons = []

def editAanstuur():
    openfiles = filedialog.askopenfilenames()
    if not openfiles:
        return 
    for file in openfiles:
        newtop = Toplevel(nGui, height=100, width=100)
        labelTitle = Label(newtop, text=file).pack()
        newButton = Button(newtop, text="save & close", command= ...).pack()
        aanstuur_files.append(file)
        aanstuur_buttons.append(newButton)
        aanstuur_frames.append(newtop)

推荐答案

按钮如何知道它属于哪个窗口?你告诉它:

How does the button know which window it belongs to? You tell it:

newButton = Button(newtop, command=lambda top=newtop: top.destroy())

顺便说一下,您在代码中将 None 分配给 newButton.这是因为你在做 newbutton = Button(...).pack(),这意味着 newbutton 获取 pack() 的值始终为 None.

By the way, you're assigning None to newButton in your code. This is because you are doing newbutton = Button(...).pack(), which means newbutton gets the value of pack() which is always None.

如果您要保存对小部件的引用,则必须在与将其放入窗口时不同的步骤中创建小部件.

If you are going to save a reference to a widget, you must create the widget in a separate step from when you place it in a window.

一个更好的解决方案是利用类和对象.创建您自己的 Toplevel 子类,该实例将为您跟踪所有子部件.例如:

An even better solution is to take advantage of classes and objects. Create your own subclass of Toplevel, and the instance will keep track of all of the subwidgets for you. For example:

class MyToplevel(Toplevel):
    def __init__(self, parent, filename, *args, **kwargs):
        Toplevel.__init__(self, parent, *args, **kwargs)
        self.filename = filename
        self.savebutton = Button(..., command=self.save)
        ...
    def save(self):
        print "saving...", self.filename
        ...
        self.destroy()
...
openfiles = filedialog.askopenfilenames()
if not openfiles:
    return 
for file in openfiles:
    newtop = MyToplevel(nGui, file, height=100, width=100)

这篇关于销毁动态创建的小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆