0

I have created a progress bar with Tkinter :

self.progressbar = ttk.Progressbar(self, orient="horizontal", length=300, maximum=100, mode="determinate")
self.progressbar.place(x=250, y=225)

And i start it when i push a Button :

self.progressbar.start(10)

But i would like that it stop itself at the end of the progress bar and then doing something else (in my case open a text file) like that :

def LaunchButton()
    self.progressbar.start(10)
    if end of progress bar
        self.progressbar.stop()
        open text file 

Thanks for helping !

Edit for @Saad :

My Launch button function :

    def launchCallBack(self):

        self.dataName = self.EditText.get("1.0", 'end-1c')
        self.dataPath = self.PathText.get(ACTIVE)
        self.dataDirectory = self.Directory.get(ACTIVE)

        print(self.dataDirectory)

        if self.dataPath == '' :
            messagebox.showinfo("error", "Please select a XML (.aird) File before launching !")
        elif '.' in self.dataName :
            messagebox.showinfo("error", "You don't have to put extension (.ctm), just put the name of your file !")
        elif self.dataName == '' :
            messagebox.showinfo("error", "Please name your Text (.ctm) File before launching")
        elif self.dataDirectory == '' :
            messagebox.showinfo("error", "Please select a directory to put your Text (.ctm) File in !")
        else :
            self.textFileCreation()

The function textFileCreation is your update function :

    def textFileCreation(self):

        self.process += 10
        self.progressbar['value'] = self.process
        if self.progressbar['value'] >= self.progressbar['maximum']:
            msg_succes = str("The file " + self.dataName + ".ctm has been created in the folder : " + self.dataDirectory)
            messagebox.showinfo("success", msg_succes)
            create_file(self.dataPath, self.dataName, self.dataDirectory)
            return
        else :
            self.after(100, self.textFileCreation())
2
  • You can find your answer here - stackoverflow.com/questions/7310511/…
    – Saad
    Apr 19, 2019 at 10:48
  • @Saad when i do like it is said in the post it don't change my progress bar, it don't stop at the end
    – Lucas
    Apr 19, 2019 at 11:18

1 Answer 1

1

Here is an example :

from tkinter import ttk
from tkinter import *

root = Tk()

P = ttk.Progressbar(root, orient="horizontal", length=300, maximum=100, mode="determinate")
P.pack()

L = Label(root, text="In Process")
L.pack()

process = 0
def update():
    global process
    process += 10
    P['value'] = process
    if P['value'] >= P['maximum']:

        L['text'] = "The Process is done!"
        # Here you can do anything when the  progress bar finishes.

        return  # This will end the after() loop
    root.after( 100, update )

update()

root.mainloop()

Also I change you function LaunchButton() to work accordingly. But do declare self.process = 0 in the __init__ of your class

Function

def LaunchButton(self):
    self.process += 10
    self.progressbar['value'] = self.process
    if self.process >= self.progressbar['maximum']:

        # Here you can do anything when the progress bar finishes.
            # open text file

        return      # This will end the after() loop

    self.after(100, self.LaunchButton)

3
  • Thanks but i adapt my code with what you said but when i press my Launch button it execute code inside the if and then complete the progress bar i don't know why
    – Lucas
    Apr 19, 2019 at 12:27
  • I have edited my original post with what i did @Saad
    – Lucas
    Apr 19, 2019 at 12:42
  • Ok i'm fucking dumb i put self.after(100, self.textFileCreation()) instead of self.after(100, self.textFileCreation) Thank you ! 2 hours for just a progress bar ...
    – Lucas
    Apr 19, 2019 at 12:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.