G’day All,
I have a little app that os.walks gathering some data. While it is working I thought it might be nice to put an Entry widget with the text “Processing” in it and change that to “Completed” when the walking was done.
The issue is the “Processing” never appears. The processing takes many seconds so it’s not like it’s over too soon to be seen.
def do_search():
txtProgress.delete(0,END)
txtProgress.insert(0, "Processing Data")
print 'do_search called'
arrayOfDirectories = [] # Store the categories here
global path
print 'The value for path = ' + path # Delete this in final
searchpath = path
print 'The value for searchpath = ' + searchpath # Delete this in final
for (searchpath, directories, files) in os.walk(searchpath):
for directory in directories:
arrayOfDirectories.append(directory) # Create an array or dirs to use for the categories
id = 1
finalJSON = '['
for eachDirectory in arrayOfDirectories:
readpath = os.path.join(path, eachDirectory, 'URLS') # Grab each list of URLs
print('readpath = ' + readpath)
if os.path.exists(readpath):
file = open(readpath) # Open the list of URLs
for lines in file: # Step through each URL in turn
ruleString = '{"id":' + str(id) + ',"enabled":true, "category":"' + eachDirectory + '","description":"' + lines + '","flagged":true,"string":"' + lines + '","name":"","javaClass":"com.untangle.uvm.node.GenericRule","blocked":true}'
#print(ruleString)
finalJSON = finalJSON + ruleString # Create a rule and add it to the final string
id = id + 1 # Increment the id after each rule
file.close() # Close the file when all have been read
It’s not a train smash if it doesn’t work but I am at a loss to understand why text is not appearing.
As always, all advice gratefully accepted.
In short, you never give your program a chance to display it. Redrawing the screen only happens as the result of a repaint event, so unless the event loop has a chance to service that event nothing will be displayed. This isn’t unique to Tkinter — all GUI toolkits work this way.
The simple solution is to call
txtProgress.update_idletasks()whenever you want the screen to be updated. This will allow events that refresh the screen to run. This is not the best solution, but it might solve your immediate problem.The best solution is to refactor your program to either do the long-running work in a separate thread or process, or break the work down to chunks that can be done one at a time with each iteration of the event loop.