python tkinter progressbar_python - Tkinter: ProgressBar with indeterminate duration

易飞文
2023-12-01

I would like to implement a progress bar in Tkinter which fulfills the following requirements:

The progress bar is the only element within the main window

It can be started by a start command without the need of pressing any button

It is able to wait until a task with unknown duration is finished

The indicator of the progress bar keeps moving as long as the task is not finished

It can be closed by a stop command without the need of pressing any stop bar

So far, I have the following code:

import Tkinter

import ttk

import time

def task(root):

root.mainloop()

root = Tkinter.Tk()

ft = ttk.Frame()

ft.pack(expand=True, fill=Tkinter.BOTH, side=Tkinter.TOP)

pb_hD = ttk.Progressbar(ft, orient='horizontal', mode='indeterminate')

pb_hD.pack(expand=True, fill=Tkinter.BOTH, side=Tkinter.TOP)

pb_hD.start(50)

root.after(0,task(root))

time.sleep(5) # to be replaced by process of unknown duration

root.destroy()

Here, the problem is that the progress bar does not stop after the 5s are over.

Could anybody help me finding the mistake?

python

python-2.7

tkinter

progress-bar

ttk

|

this question asked Aug 8 '14 at 11:10

Rickson 379 3 15

|

1 Answers

1

---Accepted---Accepted---Accepted---

Once the mainloop is active, the script wont move to the next line until the root is destroyed. There could be other ways to do this, but I would prefer doing it using threads.appear when I press a button. Clicking the "Analyse" button on the gui calls the function analyze in the script, which I've meant to be a three-stage rocket of sorts: grid() the progressbar-widget to add it to the mainframe, and start() it.

Something like this,

import Tkinter

import ttk

import time

import threading

#Define your Progress Bar function,

def task(root):

ft = ttk.Frame()

ft.pack(expand=True, fill=Tkinter.BOTH, side=Tkinter.TOP)

pb_hD = ttk.Progressbar(ft, orient='horizontal', mode='indeterminate')

pb_hD.pack(expand=True, fill=Tkinter.BOTH, side=Tkinter.TOP)

pb_hD.start(50)

root.mainloop()

# Define the process of unknown duration with root as one of the input And once done, add root.quit() at the end.

def process_of_unknown_duration(root):

time.sleep(5)

print 'Done'

root.destroy()

# Now define our Main Functions, which will first define root, then call for call for "task(root)" --- that's your progressbar, and then call for thread1 simultaneously which will execute your process_of_unknown_duration and at the end destroy/quit the root.

def Main():

root = Tkinter.Tk()

t1=threading.Thread(target=process_of_unknown_duration, args=(root,))

t1.start()

task(root) # This will block while the mainloop runs

t1.join()

#Now just run the functions by calling our Main() function,

if __name__ == '__main__':

Main()

Let me know if that helps.

|

this answer

edited Sep 15 '14 at 7:43

Benjamin 12.9k 19 101 191 answered Aug 8 '14 at 20:12

Md. Mohsin 990 6 19      Yes, this is exactly what I need. Thanks a lot! In my case, I had to replace

root.quit by

root.destroy(). Otherwise the script didn't stop properly. One last question: Is it possible to close the complete main window automatically at the end (when the status bar has stopped)? –

Rickson Aug 9 '14 at 2:55      Great, I am happy I could be of help. ... Now by "closing the complete main windows" which windows are you referring to? This code only has TKinter-root window. Are there other windows as well? –

Md. Mohsin Aug 9 '14 at 2:58      @Rickson1982 Wouldn't sys.exit() or quit() help in that case? We can add that below root.quit() in the function process_of_unkown_duration(root). Does that help? –

Md. Mohsin Aug 9 '14 at 2:58      The status bar will afterwards be integrated into the main program and

sys.exit() would close the main program as well. The only problem now is that the code above opens a new Tkinter window containing the status bar which will not be closed after the status bar stops. The code itself running through without errors. –

Rickson Aug 9 '14 at 11:03      I don't understand. The solution is quite simple. But I would need your help understanding better. There are only 3 functions. 1) ProgressBar, 2) process_of_unkown_duration, 3) Destroy Progressbar. .... Or is there a 4th function as well? Whatever is the case. When would you like to execute sys.exit()? Immediately after progressbar is closed? Or Wait for another function to excecute and then do sys.exit()? –

Md. Mohsin Aug 9 '14 at 11:08

|

show more comments

bar. I need to load largish files into my program which takes some time, so I wanted to get a progress bar to show the user the program isn't frozen loading the files. Unfortunately my progress bar does not seem to update while loading fil

 类似资料:

相关阅读

相关文章

相关问答