编辑列表框中的对象及其来源列表
Editing an object in a Listbox and the list where it comes from
我开始制作一个功能,当双击列表框中的选择时,会返回选择信息(字典)。
def OnDouble(self, event):
widget = event.widget
selection = widget.curselection()
value = widget.get(selection[0])
我想要的是能够获取返回的选择并编辑其内容。通过这样做,内容的任何更改都应该显示在列表框和它来自的列表中。
双击返回的值示例:
{'Num Tel/Cel': 'test1', 'Email': 'test1', 'Fecha de Entrega': '', 'Orden Creada:': ' Tuesday, June 23, 2015', 'Nombre': 'test1', 'Num Orden': '1'}
您可以使用此 documentation 中给出的函数来编辑列表框的选择。
例子-
widget.selection_set(<item to add>) # adds an item to the selection
或
widget.selection_clear(<item to remove>) # removes the item from the selection
的文档
from Tkinter import *
oneThing = {"Name:": "Guido", "Tel.:":"666-6969", "Email:":"foobar@lol.com"}
another = {"Name:": "Philler", "Tel.:":"111-1111", "Email:":"philler@lol.com"}
z = [oneThing, another]
root = Tk()
l = Listbox(root)
l.pack(fill = "both")
l.pack_propagate(True)
[l.insert(END, item) for item in z]
def createPerson(index):
#This is whatever function that creates stuff
def edit():
for i in range(len(labels)):
z[index][labels[i]] = entries[i].get()
print z
top.destroy()
top = Toplevel()
labels = ["Name:", "Tel.:", "Email:"]
i = 0
for text in labels:
Label(top, text = text).grid(column = 0, row = i)
i += 1
e1 = Entry(top)
e1.grid(column = 1, row = 0)
e2 = Entry(top)
e2.grid(column = 1, row = 1)
e3 = Entry(top)
e3.grid(column = 1, row = 2)
Button(top, text = "Submit", command = edit).grid(column = 1, row = 3)
entries = [e1, e2, e3]
#Return reference to toplevel so that root can wait for it to run its course
return top
def edit():
global l, z, root
# Get dictionary from listbox
sel = l.curselection()
if len(sel) > 0:
indexToEdit = z.index(eval(l.get(sel[0])))
l.delete(sel)
root.wait_window(createPerson(indexToEdit))
print z[indexToEdit]
l.insert(sel, z[indexToEdit])
Button(root, text = "Edit", command = edit).pack()
root.mainloop()
Edit:示例现在展示了一种根据用户输入动态编辑元素的方法;使用 Toplevel()
小部件接受输入。
我开始制作一个功能,当双击列表框中的选择时,会返回选择信息(字典)。
def OnDouble(self, event):
widget = event.widget
selection = widget.curselection()
value = widget.get(selection[0])
我想要的是能够获取返回的选择并编辑其内容。通过这样做,内容的任何更改都应该显示在列表框和它来自的列表中。
双击返回的值示例:
{'Num Tel/Cel': 'test1', 'Email': 'test1', 'Fecha de Entrega': '', 'Orden Creada:': ' Tuesday, June 23, 2015', 'Nombre': 'test1', 'Num Orden': '1'}
您可以使用此 documentation 中给出的函数来编辑列表框的选择。
例子-
widget.selection_set(<item to add>) # adds an item to the selection
或
widget.selection_clear(<item to remove>) # removes the item from the selection
的文档
from Tkinter import *
oneThing = {"Name:": "Guido", "Tel.:":"666-6969", "Email:":"foobar@lol.com"}
another = {"Name:": "Philler", "Tel.:":"111-1111", "Email:":"philler@lol.com"}
z = [oneThing, another]
root = Tk()
l = Listbox(root)
l.pack(fill = "both")
l.pack_propagate(True)
[l.insert(END, item) for item in z]
def createPerson(index):
#This is whatever function that creates stuff
def edit():
for i in range(len(labels)):
z[index][labels[i]] = entries[i].get()
print z
top.destroy()
top = Toplevel()
labels = ["Name:", "Tel.:", "Email:"]
i = 0
for text in labels:
Label(top, text = text).grid(column = 0, row = i)
i += 1
e1 = Entry(top)
e1.grid(column = 1, row = 0)
e2 = Entry(top)
e2.grid(column = 1, row = 1)
e3 = Entry(top)
e3.grid(column = 1, row = 2)
Button(top, text = "Submit", command = edit).grid(column = 1, row = 3)
entries = [e1, e2, e3]
#Return reference to toplevel so that root can wait for it to run its course
return top
def edit():
global l, z, root
# Get dictionary from listbox
sel = l.curselection()
if len(sel) > 0:
indexToEdit = z.index(eval(l.get(sel[0])))
l.delete(sel)
root.wait_window(createPerson(indexToEdit))
print z[indexToEdit]
l.insert(sel, z[indexToEdit])
Button(root, text = "Edit", command = edit).pack()
root.mainloop()
Edit:示例现在展示了一种根据用户输入动态编辑元素的方法;使用 Toplevel()
小部件接受输入。