Python Forum
Adding Progressbar to button
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding Progressbar to button
#3
Quote:it will run for 60 secs and after 60sec it will disappear
How are you incrementing the progress bar? Nothing will happen until the subprocess returns so you will have to use Tkinter's after method to update the progress bar if you want it to update while the subprocess is still running. A simple example using a countdown timer to simulate whatever the subprocess does, and a different progress bar because that is what is already in this program.
    try:
        import Tkinter as tk
    except:
        import tkinter as tk

    class ProgressBar():
        def __init__(self, root):
            self.root=root
            self.root.geometry("75x50+1200+100")
            tk.Button(self.root, text="Quit", bg="red", command=self.root.destroy).pack()
            self.ctr=60  ## 60 seconds
            self.start_progress_bar()
            self.start_countdown()

        def start_progress_bar(self):
            """ create a simple progress bar widget on a canvas
            """
            self.top=tk.Toplevel(self.root, takefocus=True)
            self.top.title("Progress Bar")
            self.top.geometry("+1200+300")
            self.canvas = tk.Canvas(self.top, width=261, height=60, background='lightgray')
            self.canvas.pack()

            self.rc1 = self.canvas.create_rectangle(24, 20, 32, 50, outline='white', \
                                          fill='blue')
            self.start_x=20
            self.end_x=235
            self.this_x=self.start_x
            self.one_25th = (self.end_x-self.start_x)/25.0
            rc2 = self.canvas.create_rectangle(self.start_x, 20, self.end_x, 50,
                                           outline='blue', fill='lightblue')
            self.rc1 = self.canvas.create_rectangle(self.start_x, 20, self.start_x+7, 50,
                                           outline='white', fill='blue')

            self.update_scale()

        def start_countdown(self):
            """ a separate process in a separate GUI
            """
            self.top2=tk.Toplevel(self.root, bg="lightyellow")
            self.top2.geometry("100x25+1200+200")
            self.label_ctr = tk.IntVar()
            self.label_ctr.set(self.ctr)
            tk.Label(self.top2, textvariable=self.label_ctr, width=10,
                     font=("Verdans", 15)).pack()
            self.update()

        def update(self):
            if self.ctr > 0:
                self.label_ctr.set(self.ctr)
                self.ctr -= 1
                self.root.after(1000, self.update)  ## one second
            else:
                ## sleep for one second to allow any remaining after() to execute
                ## can also use self.root.after_cancel(id)
                self.root.after(1000, self.root.destroy)

        def update_scale(self):
            self.canvas.move(self.rc1, self.one_25th, 0)
            self.canvas.update()
            self.this_x += self.one_25th
            ## reverse direction at either end
            if (self.this_x >= self.end_x-12) or self.this_x <= self.start_x+7:
                self.one_25th *= -1

            ## only call after() while the countdown is running (self.ctr > 0)
            ## to avoid a dangling after() when the program terminates
            if self.ctr > 0:
                self.canvas.after(200, self.update_scale)

    root = tk.Tk()

    PB=ProgressBar(root)
    root.mainloop()
Reply


Messages In This Thread
Adding Progressbar to button - by aniyanetworks - Feb-06-2019, 02:23 PM
RE: Adding Progressbar to button - by Larz60+ - Feb-06-2019, 05:23 PM
RE: Adding Progressbar to button - by aniyanetworks - Feb-06-2019, 08:48 PM
RE: Adding Progressbar to button - by woooee - Feb-06-2019, 08:15 PM
RE: Adding Progressbar to button - by Larz60+ - Feb-06-2019, 08:25 PM
RE: Adding Progressbar to button - by Larz60+ - Feb-07-2019, 01:15 AM
RE: Adding Progressbar to button - by aniyanetworks - Feb-07-2019, 03:53 AM
RE: Adding Progressbar to button - by Larz60+ - Feb-07-2019, 03:55 AM
RE: Adding Progressbar to button - by aniyanetworks - Feb-07-2019, 04:12 AM
RE: Adding Progressbar to button - by Larz60+ - Feb-07-2019, 11:12 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Centering and adding a push button to a grid window, TKinter Edward_ 15 5,125 May-25-2023, 07:37 PM
Last Post: deanhystad
  [Tkinter] progressbar DPaul 9 2,913 Sep-01-2021, 12:50 PM
Last Post: deanhystad
  adding button status updates to countdown counter knoxvilles_joker 7 3,462 Apr-18-2021, 01:59 AM
Last Post: knoxvilles_joker
  Progressbar with start and stop jelo34 3 5,059 Nov-30-2020, 03:36 AM
Last Post: deanhystad
  [Tkinter] Progressbar value update issue Roshan 7 3,416 Apr-22-2020, 04:02 PM
Last Post: deanhystad
  [Tkinter] Can't get progressbar to update frednet 0 1,694 Feb-24-2020, 10:53 PM
Last Post: frednet
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 5,067 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  [WxPython] Adding a Window to a Button wxPython ShashankDS 4 3,883 Apr-23-2019, 06:53 PM
Last Post: Yoriz
  tkinter- adding a new window after clicking a button built on the gui ShashankDS 2 6,667 Apr-18-2019, 12:48 PM
Last Post: ShashankDS

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020