0
Hello. I am creating a layout converter using Python 3.6.4 and Tkinter.
Among other things the GUI must have a progress bar that updates its value with each interaction of the conversion process, for example, with each processed line of the file being converted, the progress bar must be updated.
The problem I am facing is that the "program" hangs while running the conversion and the bar is not updated. The bar only updates at the end of the conversion operation.
The basic part of the script is this (I have removed everything that is not relevant and left only the essential to see where the problem is or where I am missing):
import sys
from time import sleep
from tkinter import *
from tkinter.ttk import Progressbar
class Gui:
def __init__(self):
self.Window = Tk()
self.Window.geometry('{0}x{1}'.format(600, 400))
self.progress = StringVar()
self.progress.set(0)
self.progressBar = Progressbar(self.Window, maximum=100, orient=HORIZONTAL, variable=self.progress)
self.progressBar.grid(row=0, column=0, sticky=W + E)
self.startButton = Button(self.Window, text='Iniciar', command=self.start)
self.startButton.grid(row=0, column=2)
def start(self):
for t in range(0,100):
self.progress.set(t)
sleep(0.1)
def run(self):
self.Window.mainloop()
return 0
if __name__ == '__main__':
Gui = Gui()
sys.exit(Gui.run())
I’m a beginner in Python but I have long experience (fifteen years) with PHP and etc.
As for the use of time.Sleep(), I used only for example code, but it is not used in production code.
– Everton da Rosa
yes - but you understood the answer? You call the function updating the progressibar to a fixed time interval, with the method
.after
- and it checks the attribute of "how complete" (but will not update it, as in this example)– jsbueno
The error is you can’t make a loop straight to update the property - you have to return to the mainloop so the work can walk.
– jsbueno
I captured the essence of what you put (I think), the question is, the code I have is a layout converter. It takes some data (input directory, output directory, conversion format), and when the user clicks on the convert button, the python should take file by file from the input directory, read line by line from the file and convert it into a certain format and write; and still: to each processed file (or could be to each row) update the progress bar. I’m unable to visualize how to do this with Tk.after(). I have to study the subject further!
– Everton da Rosa
in which case, I think it might be better to call the
update_idletasks
even after every x lines. Despite what I wrote, in python we have to keep in mind that "practicality Beats purity". Now, what you can do is keep things separate: keep a 4 =/5 linahs method just to update the progress bar, like the one in the example, using theafter
and in its main processing use the;update_idletasks
- this way you don’t need to mix the logic of updating the progress bar with the logic of your application’s tasks.– jsbueno