【问题标题】:How do i remove a item from a Listbox when it has been chosen and a button has been pressed选择并按下按钮后,如何从列表框中删除项目
【发布时间】:2017-07-06 15:17:21
【问题描述】:

我正在创建一个在冰箱上使用的应用程序,以便用户可以通过 GUI 屏幕上的列表框查看冰箱中的物品,然后当他们删除一个项目时,他们可以按下列表上的那个项目框,然后按删除,该项目将从列表框中删除。

但是,我遇到了一个问题,即如何使用按钮从列表中删除项目(如果有任何意义)。

这是我的代码;

  def Pud():
        window = Tk()
        window.title('Listbox Example')

        def dialog():
***#########Somehere around here i belive should be where the item is removed from the list box########***

            box.showinfo('Selection','Item taken from the fridge: \n'+ \
            listbox.get(listbox.curselection()))

        frame = Frame(window)

        listbox = Listbox(window)
        listbox.pack()
        Items_In_Fridge = ['Milk', 'Butter']
        for widget in Items_In_Fridge:
            listbox.insert(0, widget)

        btn = Button(window,text = 'Remove This Item',command=dialog)

        btn.pack(side = RIGHT,padx = 5)
        listbox.pack(side = LEFT)

        window.mainloop()

我知道代码应该在哪里但是我不确定代码是什么或如何根据我的需要来格式化它,我已经尝试了多种方法来尝试完成它但没有成功。

任何帮助将不胜感激我正在使用 python 3

【问题讨论】:

    标签: python python-3.x user-interface tkinter listbox


    【解决方案1】:

    捕捉对话的响应。如果用户说“是”,则删除该项目。

    response = messagebox.askquestion ('Delete Item','Delete the item taken from the fridge?\n'+ \
        listbox.get(listbox.curselection()))
    if response == 'yes':
        listbox.delete(listbox.curselection())
    

    【讨论】:

    • 谢谢,老实说我没有想到一个提问框。
    猜你喜欢
    • 1970-01-01
    • 2018-12-29
    • 2014-03-03
    • 1970-01-01
    • 2021-09-17
    • 2016-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多