import tkinter as tk import tkinter.ttk as ttk class Controller(object): def __init__(self, app): self.app = app self.count = 0 self.running = False self.interrupt = False def run_process(self): steps = 42 delta = 100 / steps print("run {}".format(self.count)) self.set_running() if self.count == steps or self.interrupt: print(self.count) self.running = False self.finish() return 0 else: self.count += 1 self.app.after(1, lambda : self.app.p.step(delta)) self.app.after(50, self.run_process) return 1 def set_interrupt(self): self.interrupt = True self.app.b.configure(text="Reset", command=self.set_reset) def set_running(self): if not self.interrupt: self.running = True self.app.b.configure(text="Abort", command=self.set_interrupt) def set_reset(self): self.count = 0 self.interrupt=False self.app.pbar_var.set(0) self.app.b.configure(text="Start", command=self.run_process) def finish(self): print("finished.") self.app.b.configure(text="Restart", command=self.set_reset) class Application(tk.Tk): def __init__(self): super().__init__() self.con = Controller(self) self.create_control_variables() self.create_widgets() def create_control_variables(self): self.pbar_var = tk.IntVar() def create_widgets(self): self.p = ttk.Progressbar(master=self, length="200", maximum=100, mode="determinate", variable=self.pbar_var) self.p.grid(row=0, column=0) self.b = ttk.Button(master=self, text="Start", command=self.con.run_process) self.b.grid(row=1, column=0) # -------------------------- # Execution # -------------------------- if __name__ == "__main__": app = Application() app.mainloop()