【问题标题】:Editing an object in a Listbox and the list where it comes from编辑列表框中的对象及其来源列表
【发布时间】:2015-09-11 01:58:36
【问题描述】:

我开始制作一个函数,当双击列表框中的选择时,会返回选择信息(字典)。

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'}

【问题讨论】:

  • 我正在考虑使用创建此词典的相同功能作为编辑它们的一种方式。这可能吗?

标签: python tkinter listbox python-3.4


【解决方案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

selection_set 的文档 - here selection_clear - here 的文档

【讨论】:

    【解决方案2】:
    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()
    

    编辑:示例现在展示了一种根据用户输入动态编辑元素的方法;使用Toplevel() 小部件接受输入。

    【讨论】:

    • 我想编辑字典。使用相同的功能创建。我有这个 tkwindow,用户在其中输入数据(姓名、电子邮件、电话号码等)。这样,也许当窗口弹出时,在输入字段中,我要编辑的选择的内容已经显示出来了..
    猜你喜欢
    • 1970-01-01
    • 2019-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-30
    • 2021-11-16
    • 1970-01-01
    相关资源
    最近更新 更多