从列表框Python中删除项

2024-09-30 01:34:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图从一个列表框中删除一个与我的checkbutton函数连接的项。在

ShrimpTempVar=DoubleVar()
ShrimpTempCheck=Checkbutton(menu,variable = ShrimpTempVar, onvalue=5.99, offvalue=0,command=ShrimpTempuraOrder) 

def ShrimpTempuraOrder():
    if ShrimpTempVar.get()==5.99:
        qty=int(QTY2Var.get())
        finalprice=ShrimpTempVar.get()*qty
        listbox.insert(END, "Shrimp Tempura")
        listboxprice.insert( END, finalprice)
    elif ShrimpTempVar.get()==0:
        listbox.delete(0,END)
        listboxprice.delete(0,END)

我希望取消选择该项时将其删除。 在另一篇帖子中,有人建议这样做:

^{pr2}$

但它说Listbox在Items中没有属性

另外,我不知道索引号,因为它会随着我选择不同复选按钮的顺序而改变。在

谢谢你的帮助!在


Tags: 函数getdeleteendqtyinsertlistbox列表框
1条回答
网友
1楼 · 发布于 2024-09-30 01:34:54

Question: Remove Item from Listbox Python, if I do not know the index number


文件

  • Python Documentation - Common Sequence Operations

    s.index(x[, i[, j]])    
    index of the first occurrence of x in s (at or after index i and before index j)    
    

    Note: (8) index raises ValueError when x is not found in s. Not all implementations support passing the additional arguments i and j. These arguments allow efficient searching of subsections of the sequence. Passing the extra arguments is roughly equivalent to using s[i:j].index(x), only without copying any data and with the returned index being relative to the start of the sequence rather than the start of the slice.

  • The Tkinter Listbox Widget - get(first, last=None)

    This function returns the string corresponding to the given index (or the strings in the given index range). Use get(0, END) to get a list of all items in the list.


  1. 使用Listbox项的标签字符串获取其索引

    label = "Shrimp Tempura"
    idx = listbox.get(0, tk.END).index(label)
    
  2. 删除此索引处的Listbox

    listbox.delete(idx)
    

使用Python:3.5-TkVersion:8.6测试

相关问题 更多 >

    热门问题