所以我創建了一個待辦事項串列型別,用戶輸入他們當天的任何任務,然后他們可以給自己一個時間限制,完成任務后他們可以檢查它,這將使字體顏色變為綠色,何時計時器結束時會將其從串列框中洗掉我的問題是如何制作它以便串列框將選擇帶有綠色字體的任何任務然后將其洗掉
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
我檢查了是否有任何已完成的任務,但我不知道任何將遍歷串列框并檢查是否有任何專案的字體顏色為綠色的函式
uj5u.com熱心網友回復:
您可以以相反的順序瀏覽串列中的每個專案,并使用 獲取專案的前景色itemcget()。如果前景色為green,則洗掉該專案:
...
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 回圈。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/401001.html
