由于我是初学者,因此我一直在尝试创建最简单的进度条,只是为了检查线程的运行情况。似乎可以正常工作,但是当我关闭进度栏窗口时出现错误(“ Tkinter Tcl_AsyncDelete:异步处理程序被错误的线程删除”)。有办法避免吗? 我也不认为这是最好的编码,因为我使用的是全局变量,因此我很乐意接受任何有关如何使其更简单,更pythonic的建议。
from tkinter import *
import time
import threading
progress_percentage = 10
def progress_percentage_changer():
global progress_percentage
print("started working")
time.sleep(1) # lot of work ...
progress_percentage = 50
time.sleep(1) # lot of work ...
progress_percentage = 70
time.sleep(1) # lot of work ...
progress_percentage = 100
print("job done")
def progess_bar():
global progress_percentage
def update_progress_line(progress_line_id, canvas): # it just updates the bar
canvas.coords(progress_line_id, 0, 5, progress_percentage, 5)
root.after(1100, update_progress_line, progress_line_id, canvas)
root = Tk()
canvas = Canvas(root, bg="dark grey", height=10, width=100) # creating canvas
progress_line_id = canvas.create_line(0, 5, progress_percentage, 5, width=11,
fill="green") # creating the green progress line
canvas.grid(row=1)
update_progress_line(progress_line_id, canvas)
root.mainloop()
if __name__ == '__main__':
t1 = threading.Thread(target=progess_bar)
t2 = threading.Thread(target=progress_percentage_changer)
t1.start()
t2.start()
t1.join()
t2.join()