#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2017
    Posts
    85
    Rep Power
    2

    [Python 3 + Tkinter] Progressbar works not correctly, help needed!


    Hey Python Programmers and GUI Envolvers....,

    I try to create an progressbar thad load an serie of numbers (called i) in an for loop...,
    but it don't works correctly yet...


    This is my script, writted in python 3, pyScripter:
    Code:
    from tkinter import *
    from tkinter.ttk import *
    
    class Window(Frame):
        def __init__(self, master=None):
            Frame.__init__(self, master)
            self.master = master
            self.init_window()
    
        def init_window(self):
            self.master.title('Loading Bar')
            self.b = Button(self.master, text = 'Press to Range', command = self.loadBar)
            self.b.pack(fill = X, side = TOP)
            self.pb = Progressbar(self.master, length = 12, orient = HORIZONTAL, takefocus = True)
    
            self.l = Label(self.master)
            self.l.pack(fill = X, side = BOTTOM)
    
        def loadBar(self):
            r = 400
            for i in range(r):
                self.pb.pack(fill = X, side = TOP)
                self.l.config(text = str(i))
                self.pb.start()
                self.pb.step(i)
                self.pb.update()
    
            self.pb.stop()
    
    
    
    if __name__ == "__main__":
        root = Tk()
        app = Window(root)
        app.mainloop()
    I want thad my progressbar load the numbers one times (like an real loading bar yet), but
    it load per/number again, number for number.... i want that it load one times with all numbers...
    But my progressbar starts again by each number...

    Can anyone correct my code, just i hope learn about program an normally progressbar, thad
    load one times for all numbers yet ?.... Thanks for help, Jamie.
  2. #2
  3. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,974
    Rep Power
    510
    This comment is more of general use than a specific answer to your trouble.
    Progress bar should simply say

    GO ELSEWHERE, DO SOMETHING USEFUL FOR 10 MINUTES
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    667
    Rep Power
    40
    The for loops through the numbers faster than you can see. This uses a 1/4 second time out so you can see it. Since you did not set maximum, it uses the default of 100 so your code will only display in increments of 100, 1-100, new bar for 101-200, etc.
    Code:
    from tkinter import *
    from tkinter.ttk import *
    
    class Window():
        def __init__(self, master=None):
            self.master = master
            self.increment = 0
            self.init_window()
    
        def init_window(self):
            self.master.title('Loading Bar')
            self.b = Button(self.master, text = 'Press to Range',
                            command = self.load_bar)
            self.b.pack(fill = X, side = TOP)
            self.pb = Progressbar(self.master, length = 12,
                                  orient = HORIZONTAL, maximum=400,
                                  takefocus = True)
            self.l = Label(self.master)
            self.l.pack(fill = X, side = BOTTOM)
    
        def advance_bar(self):
            self.l.config(text = str(self.increment))
            self.pb.step(1)
            self.increment += 1
            if self.increment < 400:
                self.master.after(250, self.advance_bar)
            else:
                self.master.quit()
    
    
        def load_bar(self):
            self.pb.pack(fill = X, side = TOP)
            self.advance_bar()
    ##            self.pb.start()
    ##            self.pb.step(i)
    ##            self.pb.update()
    
    ##        self.pb.stop()
    
    
    
    if __name__ == "__main__":
        root = Tk()
        app = Window(root)
        root.mainloop()

IMN logo majestic logo threadwatch logo seochat tools logo