从列表框中删除选择,并将其从提供选择的列表中删除 [英] Removing a selection from a listbox, as well as remove it from the list that provides it

查看:74
本文介绍了从列表框中删除选择,并将其从提供选择的列表中删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用以下代码从列表框中删除所选内容并将其从包含其的列表中删除?列表框中的选择是我存储在列表中的字典.

How can I use the following code to delete a selection from a listbox and removing it from the list the contains it also? The selections in the listbox are dictionaries which I store in a list.

.................code..............................
self.frame_verDatabase = Listbox(master, selectmode = EXTENDED)
        self.frame_verDatabase.bind("<<ListboxSelect>>", self.OnDouble)
        self.frame_verDatabase.insert(END, *Database.xoomDatabase) 
        self.frame_verDatabase.pack()

        self.frame_verDatabase.config(height = 70, width = 150)

    def OnDouble(self, event):
        widget = event.widget
        selection=widget.curselection()
        value = widget.get(selection[0])
        print ("selection:", selection, ": '%s'" % value)

示例:当我在列表框中进行选择时,将返回以下数据:

Example: When I make a selection in the listbox, this data gets returned:

selection: (2,) : '{'Fecha de Entrega': '', 'Num Tel/Cel': 'test3', 'Nombre': 'test3', 'Num Orden': '3', 'Orden Creada:': ' Tuesday, June 23, 2015', 'Email': 'test3'}'

推荐答案

from tkinter import *
things = [{"dictionaryItem":"value"}, {"anotherDict":"itsValue"}, 3, "foo", ["bar", "baz"]]
root = Tk()
f = Frame(root).pack()
l = Listbox(root)
b = Button(root, text = "delete selection", command = lambda: delete(l))
b.pack()
l.pack()

for i in range(5):
    l.insert(END, things[i])

def delete(listbox):

    global things
    # Delete from Listbox
    selection = l.curselection()
    l.delete(selection[0])
    # Delete from list that provided it
    value = eval(l.get(selection[0]))
    ind = things.index(value)
    del(things[ind])
    print(things)

root.mainloop()

为清楚起见进行了编辑.由于在这种情况下, listbox 仅包含 dict 个对象,因此我只是 eval listbox 提取的值,在列表对象中获取其索引,然后将其删除.

Edited for clarity. Since the listbox in this case only included dict objects I simply eval the value that is pulled from the listbox, get its index inside the list object, and delete it.

从第二个注释到 print 语句的所有操作都可以在一行中完成,如下所示:

Everything from the second comment up to the print statement can be accomplished in one line as follows:

del(things[things.index(eval(l.get(selection[0])))])

如果您想发挥创造力.

这篇关于从列表框中删除选择,并将其从提供选择的列表中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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