当窗口失去焦点时,tkinter进度条不会更新



我有下面的代码,其中run是一个执行任务的方法,它在执行任务时递增地更新进度条。

window = tk.Tk()
button = tk.Button(text = 'Analyze Data', width=15, height=3,bg='gray', fg='black')
button.bind("<Button-1>", run)
button.pack(pady=10)
progress = ttk.Progressbar(window, orient = tk.HORIZONTAL, mode = 'determinate')
progress.pack(pady=10)
window.mainloop()

它工作得很好,除了如果窗口失去焦点,进度条将不再更新(即使窗口重新获得焦点(。为什么会发生这种情况?即使窗口失去焦点,是否有办法更新进度条?

我用更新运行功能中的进度条

progress['value'] = progress['value']+1
window.update_idletasks()

对我来说,在更新进度条值后从window.update_idletasks((更改为window.update((解决了问题

最新更新