如何从 tkinter 的列表框中删除具有特定特征的元素 python

how to delete elements with a specific traits from a listbox in tkinter python

所以我正在创建一个待办事项列表类型,用户可以在其中输入他们当天的任何任务,然后他们可以给自己一个时间限制,完成任务后他们可以检查它,这将改变字体颜色变为绿色,当计时器结束时会将其从列表框中删除我的问题是如何才能使列表框select所有字体为绿色的任务然后将其删除

count_task=0 #to check if the task is done
def check_task():
    global count_task
    todo_list.itemconfig(
        todo_list.curselection(),
        fg="green")
    count_task+=1
    todo_list.selection_clear(0, END)
def uncheck_task():
    global count_task
    todo_list.itemconfig(
        todo_list.curselection(),
        fg="white")
    count_task-=1
    todo_list.selection_clear(0, END)
def timer():
    #placing all entry widgets label and buttons for a timer
    def countdowntimer():
        try:
            times=int(hrs.get())*3600 + int(mins.get())*60 + int(sec.get())
        except:
            print("Input the correct value")
        while times > -1:
            minute, second = (times // 60, times % 60)
            hour = 0
            if (minute > 60):
                hour, minute = (minute//60 , minute%60)
            sec.set(second)
            mins.set(minute)
            hrs.set(hour)
            root1.update()
            time.sleep(1)
            if(times == 0):
                sec.set('00')
                mins.set('00')
                hrs.set('00')
                if(count_task==0):
                    mb.showwarning("Task not accomplished","Focus on your work  no task has been completed yet")
                elif(count_task>0):   #the problem I have is here
                   
            times -= 1

我已经检查过是否有任何已完成的任务,但我不知道有任何函数会遍历列表框并检查是否有任何项目的字体颜色为绿色

您可以按相反顺序浏览列表中的每个项目,并使用 itemcget() 获取项目前景颜色。如果前景色是绿色,删除项目:

...
elif(count_task>0):   #the problem I have is here
    # loop through all item in the listbox in reverse order
    for i in range(todo_list.size()-1, -1, -1):
        # if item foreground color is green, delete it
        if todo_list.itemcget(i, 'foreground') == 'green':
            todo_list.delete(i)
...

请注意,正如我在回答您的其他问题时所说,应避免在 tkinter 应用程序的主线程中使用 while 循环。