我想不出来。我还在循环中尝试了after()
。目标是通过使用画布(和更新标签)创建进度条。问题是,整个窗口在“循环”之后生成
from tkinter import *
import time
root = Tk()
root.geometry("350x350")
root.maxsize(350,350)
root.minsize(350, 350)
root.attributes("-transparentcolor", "red")
can = Canvas(root, bg="red", highlightthickness=0)
can.place(relwidth=1, relheight=1)
ext = 3.59
for x in range(100):
can.create_arc(10, 10, 340, 340, extent=ext, fill="#a7bee6", width=0)
#time.sleep(0.1)
ext += 3.59
can.create_oval(30, 30, 320, 320, fill="red", width=0)
can.create_oval(50, 50, 300, 300, fill="#1f1f1f", width=0)
lbl = Label(can, bg="#1f1f1f", fg="#a7bee6", text="%", font=("Consolas", 30))
lbl.pack(pady=150)
for x in range(101):
lbl.configure(text=f"{x}%")
time.sleep(0.1)
mainloop()
这应该可以通过使用
after(ms,func)
实现,只需从主块中删除所有执行增量的代码,并将其移动到函数中,然后只需使用after
,如:当您使用
time.sleep()
时,它会阻止mainloop
处理或更新事件,因此GUI将保持冻结状态,直到time.sleep()
或任何其他循环完成虽然我没有讨论progressbar的功能,但似乎在最后还有一点地方可以覆盖,以显示100%的圆。我认为将
ext
增加3.60可以解决这个问题(ext+=3.60
)相关问题 更多 >
编程相关推荐