5 Replies - 927 Views - Last Post: 28 June 2016 - 10:31 AM Rate Topic: -----

#1 ToastyJones  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 09-June 16

Trying to Progress with Progress Bar

Posted 23 June 2016 - 08:46 AM

Hi,

I`m learning how to use a progress bar. Or lets say I`m trying to.
I have a window, 2 progress bars and a button.
My first attempt seems to work as expected.
Click button and pbar displays via the tests function.
My second attempt is I call function timed_call after 6 seconds via
id = root.after(6000, timed_call)


The delay is working ok but what I would like to do is show the delay via
pbar2. That is, pbar2 displays the 6 seconds during the wait for timed_call.


My code is below

Cheers, ToastyJones

# Progress Bar learning

from tkinter import*
from tkinter import ttk
import time

root = Tk()

root.minsize(500, 500)


def timed_call():
    print("hello")
    root.after_cancel(id)


def tests():
    pbar.start()
    for i in range(50):
        time.sleep(0.1)
        pbar.step(1)
        pbar.update()
    
    pbar.stop()

test = Button(text="Press to start", command=tests)
test.place(x=50, y=200)

pbar = ttk.Progressbar(orient="horizontal", mode='determinate', length=100)
pbar.place(x=50, y=150)

pbar2 = ttk.Progressbar(orient="horizontal", mode='determinate', length=100)
pbar2.place(x=50, y=100)

id = root.after(6000, timed_call)


root.mainloop()




Is This A Good Question/Topic? 0
  • +

Replies To: Trying to Progress with Progress Bar

#2 CurlyJoe  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 97
  • Joined: 19-September 13

Re: Trying to Progress with Progress Bar

Posted 23 June 2016 - 10:01 AM

You can either [1] call timed_call(), which will update the progress bar and call itself after some time, so if you want to update the bar every 1/2 second, timed_call() will contain a statement--> root.after(500, timed_call) and of course will only do this 12 times=6 seconds. You can also [2] issue all "afters" at one time, i.e. 500, 1000, 1500, etc.
for ctr in range(6):
    root.after(500*(ctr+1), timed_call)
Note that programming more complex things, like a GUI, is easier to program when you use classes, so before going any further I would suggest that you next learn how to program using a class structure.
## A simple example that I had lying around 
##
try:
    import Tkinter as tk     ## Python 2.x
except ImportError:
    import tkinter as tk     ## Python 3.x

import ttk

class TestProgress():
    def __init__(self):
        self.root = tk.Tk()
        self.root.title('ttk.Progressbar')
 
        self.increment = 0
        self.pbar = ttk.Progressbar(self.root, length=300)
        self.pbar.pack(padx=5, pady=5)

        self.progress=tk.StringVar()
        tk.Label(self.root, textvariable=self.progress). pack()

        self.root.after(100, self.advance) 
        self.root.mainloop()

    def advance(self):
        # can be a float
        self.pbar.step(5)
        self.increment += 5
        self.progress.set(self.increment)
        if self.increment < 100:
            self.root.after(500, self.advance) 
        else:
            self.root.quit()
 
 
TP=TestProgress() 

This post has been edited by CurlyJoe: 23 June 2016 - 10:11 AM

Was This Post Helpful? 1
  • +
  • -

#3 ToastyJones  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 09-June 16

Re: Trying to Progress with Progress Bar

Posted 23 June 2016 - 01:05 PM

Hi,

Thanks CurlyJoe for response.

I've adaptated your code ( which helps me understand what's happening ) to get
a working version, I got a problem with the progress bar just looping but I've got
it working. The code is really rough. So I'll polish it and play around with it and
learn lots, then post the results. Due to work the earliest I can post is Monday.

Btw, your code highlighted one of my biggest headaches with Python, the different
versions, I'm using v3.5.1, it seems whenever I find an example of what I am trying
learn, I type it in and get errors due to it being an earlier version.

Cheers, ToastyJones
Was This Post Helpful? 0
  • +
  • -

#4 ToastyJones  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 09-June 16

Re: Trying to Progress with Progress Bar

Posted 27 June 2016 - 04:29 AM

Hi,
I`ve been playing around with the progress bar. My code is just for the version that shows time passing.
I`ve tried changing colour/color of the bar using Style(), not easy. I`ve also tried to display the `value` of the bar as it increments, naaaahhhh!
So below is my simple version that works. I understand CurlyJoe about classes but python `self` is something I need to read more about.
Its just a 10 second timing shown by a progress bar, because I'm unable to find out how to use the progress bar `value` I use
a variable time_length to increment the 10 second count. The print statements are there so I can see the program flow.

Cheers ToastyJones

# Progress Bar learning

from tkinter import*
from tkinter import ttk

root = Tk()
root.minsize(500, 500)

global id, time_length
time_length = 0


def end_timing():
    print("Timing Ended")
    root.after_cancel(id)
    

def test():
    global id, time_length
    pbar.step(10) # increments of 10 so I can see the bar move, this is relevent to its length
    pbar.update()
    time_length = time_length + 1
    id = root.after(1000, test) # update each second
    print("I`m here")
    if time_length == 10:
        end_timing()

        
myinfo = Label(text="10 second timing", )
myinfo.place(x=50, y=200)

pbar = ttk.Progressbar(orient="horizontal", mode='determinate', length=100, maximum = 100)
pbar.place(x=50, y=100)

test()

root.mainloop()



Was This Post Helpful? 0
  • +
  • -

#5 CurlyJoe  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 97
  • Joined: 19-September 13

Re: Trying to Progress with Progress Bar

Posted 27 June 2016 - 10:17 AM

self just refers to the class instance's name space, so self.function or self.var refer to objects within the class. See "What is self" at http://www.freenetpa...ld/tutclass.htm
Was This Post Helpful? 1
  • +
  • -

#6 ToastyJones  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 09-June 16

Re: Trying to Progress with Progress Bar

Posted 28 June 2016 - 10:31 AM

Hi,

Thanks CurlyJoe for the help, I've checked the link for info on self.

Cheers ToastyJones
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1