【问题标题】:how to delete elements with a specific traits from a listbox in tkinter python如何从 tkinter python 的列表框中删除具有特定特征的元素
【发布时间】:2021-12-31 07:43:14
【问题描述】:

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

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

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

【问题讨论】:

  • 最好提供minimal reproducible example,否则我们不知道您尝试过什么并且很难知道该怎么做。
  • 对不起,只是一分钟

标签: python tkinter listbox


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

【讨论】:

  • 哦,好的,非常感谢你帮了我很大的忙,希望你有一个美好的新年
猜你喜欢
  • 1970-01-01
  • 2022-11-14
  • 1970-01-01
  • 2020-07-23
  • 2016-08-16
  • 2021-07-20
  • 2019-02-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多