|
发表于 2021-8-27 23:03:05
|
显示全部楼层
- from tkinter import *
- from tkinter import ttk
- from tkinter import messagebox
- class App(Tk):
- def __init__(self, geo, title):
- super().__init__()
- self.geometry(geo)
- self.title(title)
- self.btn = Button(self, text='开始任务', command=self.pbar_update)
- self.btn.pack(pady=20)
- self.pbar = ttk.Progressbar(self, length=400, maximum=400)
- self.pbar['value'] = 0
- self.pbar.pack(pady=20)
- self.cid_pbar = None
- def pbar_update(self):
- '''每半秒钟更新一次进度条'''
- print(self.pbar['value'])
- self.pbar['value'] += 20
- if self.pbar['value'] >= 400:
- messagebox.showinfo(message="任务已完成!!")
- self.pbar['value'] = 0
- else:
- # 没超过400 (最大值)的话,就循环更新进度
- if self.cid_pbar is not None:
- self.after_cancel(self.cid_pbar)
- self.cid_pbar = self.after(500, self.pbar_update)
- else:
- self.cid_pbar = self.after(500, self.pbar_update)
-
- if __name__ == "__main__":
- app = App('500x500', '任务进度条-test')
- app.mainloop()
-
复制代码 |
|