Progressbar with start and stop - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: Progressbar with start and stop (/thread-31199.html) |
Progressbar with start and stop - jelo34 - Nov-27-2020 Hello,I a Python beginner and am unable to code a scrpit coding for for a window with a start button and a stop button able to start an stop a progressbar in tkinter. My script start the proression but cannot stop it. Thanks for your help!! import tkinter as tk from tkinter import * def create_window(): from tkinter import ttk import time def start(): for x in range (5): myprogress['value']+=20 root.update_idletasks() time.sleep(1) print(myprogress['value']) def stop(): myprogress.stop() root= Tk() root.geometry("660x400") myprogress=ttk.Progressbar(root, orient=HORIZONTAL, length=300, mode='indeterminate') myprogress.pack(pady=20) mybutton=Button(root, text="start", command=start) mybutton.pack(pady=20) mybuttonstop=Button(root, text="stop", command=stop) mybuttonstop.pack(pady=20) root.mainloop() root = tk.Tk() b = tk.Button(root, text="Create new window", command=create_window) b.grid(row=4, column=3, sticky=W+E, padx=10, pady=20) root.mainloop() RE: Progressbar with start and stop - deanhystad - Nov-27-2020 You don't want to use a loop and wait. That blocks the GUI from responding to things like typing or mouse clicks. You need to generate periodic events that call a function to update the progress bar. Read about tkinter.after. RE: Progressbar with start and stop - joe_momma - Nov-28-2020 in your script you use stop() but you didn't use start import tkinter as tk from tkinter import * def create_window(): from tkinter import ttk import time def start(): myprogress.start() def stop(): myprogress.stop() root= Tk() root.geometry("660x400") myprogress=ttk.Progressbar(root, orient=HORIZONTAL, length=300, mode='indeterminate', maximum=100) myprogress.pack(pady=20) mybutton=Button(root, text="start", command=start) mybutton.pack(pady=20) mybuttonstop=Button(root, text="stop", command=stop) mybuttonstop.pack(pady=20) root.mainloop() root = tk.Tk() b = tk.Button(root, text="Create new window", command=create_window) b.grid(row=4, column=3, sticky=W+E, padx=10, pady=20) root.mainloop()example of after and after_cancel: from tkinter import * from random import shuffle, random, choice from tkinter.messagebox import * from tkinter.ttk import Progressbar import logging, sys logging.basicConfig(level= logging.DEBUG) #logging.disable(logging.CRITICAL) class Start_Stop(Frame): def __init__(self, parent=None): self.parent= parent Frame.__init__(self, self.parent) self.pack(expand=YES, fill=BOTH) self.canvas= Canvas(self) self.canvas.config(width= 800, height= 700, bg='skyblue') self.canvas.pack(expand=YES, fill=BOTH) self.btn= Button(self.canvas, text='Start', command= self.get_entry) self.btn.place(x=400,y=600) self.progress_bar= Progressbar(self.canvas, orient='horizontal', length=500, mode='determinate') self.progress_bar.place(x=50, y=350) self.btn2= Button(self.canvas, text='Stop', command= self.stop_timer, state='disabled') self.btn2.place(x=200,y=600) self.btn3= Button(self.canvas, text='reset', command= self.reset, state='disabled') self.btn3.place(x=300,y=600) self.bind_all('<Key>', self.key) self.g_count=0 self.timer= 18 self.start_time=0 def key(self, event): self.g_count +=1 message= 'count:{0} key:{1} num:{2} state:{3}'.format(self.g_count, event.keysym,event.keysym_num, event.state) logging.debug(message) def get_entry(self): self.btn2.config(state='normal') self.progress_bar['value']=0 self.progress_bar['maximum']= self.timer self.end_time= self.timer self.read_time() def read_time(self): self.start_time +=1 self.progress_bar['value']= self.start_time if self.start_time < self.end_time: self.pots= self.after(1000, self.read_time) else: self.btn.config(state='disabled') self.btn2.config(state='disabled') self.btn3.config(state='normal') def reset(self): self.start_time=0 self.btn.config(state='normal') self.btn3.config(state='disabled') self.progress_bar['value']=0 def stop_timer(self): self.after_cancel(self.pots) if __name__ == '__main__': root= Tk() Start_Stop(root) root.mainloop() RE: Progressbar with start and stop - deanhystad - Nov-30-2020 import tkinter from tkinter import ttk task_running = False def update_progress(): global task_running if task_running: if progress_bar['value'] == 100: progress_bar['value'] = 0 else: progress_bar['value'] += 5 root.after(100, update_progress) def start_stop(): global task_running if task_running: task_running = False else: task_running = True update_progress() root = tkinter.Tk() progress_bar = ttk.Progressbar(root, length=100, orient=tkinter.HORIZONTAL, mode="determinate") progress_bar.grid(row=0, column=0) tkinter.Button(root, text="Push me", command = start_stop).grid(row=1, column=0) root.mainloop() |