このプロジェクトに最後の仕上げを加えるだけです。私は Bryan の両方の提案を適用し、.update_idletasks() メソッドとスレッド化を使用しました。最終的なコードは目的どおりに機能し、次のようになります。
from Tkinter import *
import os
import threading
from tkFileDialog import askdirectory
#####################################
# Put the grunt stuff up here #
#####################################
def loadpath():
print 'loadpath called'
global path
path = askdirectory()
txtPath.delete(0, END)
txtPath.insert(0, path)
def update_the_status():
txtProgress.delete(0,END)
txtProgress.insert(0, "Processing Data")
txtProgress.update_idletasks()
def do_the_search():
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
finalJSON = finalJSON + ']' # Close the JSON array
outputPath = os.path.join(os.path.dirname(path), 'Blacklist.json')
print('Output path = ' + outputPath)
outputFile = open(outputPath, 'w')
outputFile.write(finalJSON)
txtProgress.delete(0,END)
txtProgress.insert(0,"Process Complete")
outputFile.close()
def do_search():
WriteThread = threading.Thread(target=update_the_status())
CalcThread = threading.Thread(target=do_the_search())
WriteThread.start()
CalcThread.start()
def do_quit():
print 'do_quit called'
sys.exit()
#####################################
# Build the interface #
#####################################
# Some global variables
path = ''
# Create the application window and give it a title.
main = Tk()
main.geometry('600x400')
main.title('Blacklist JSON array builder')
# Populate the window with widgets.
lbSpace1 = Label(main, text='')
lbSpace1.grid(row=0, column=0, columnspan=3)
lbDesc = Message(main, width=800, text='After you have unzipped the .tar file select the blacklist folder \nthat has been created using the browse button. Then click start. The Blacklist.json \nfile will be created in the same directory as the Blacklist folder.')
lbDesc.grid(row=1, column=0, columnspan=4, pady=10)
lbPath = Label(main, text='Directory')
lbPath.grid(row=2, column=0, pady=10)
txtPath = Entry(main, width=50)
txtPath.grid(row=2, column=1)
pbPath = Button(main, text='Browse', command=loadpath)
pbPath.grid(row=2, column=2)
lbSpace2 = Label(main, text='')
lbSpace2.grid(row=3, column=0, columnspan=3)
pbStart = Button(main, text='Begin', command=do_search)
pbStart.grid(row=4, column=1, sticky=W, pady=20)
pbQuit = Button(main, text='Quit', command=do_quit)
pbQuit.grid(row=4, column=2, sticky=W)
lbSpace3 = Label(main, text='')
lbSpace3.grid(row=5, column=0, columnspan=3)
txtProgress = Entry(main, width=50)
txtProgress.grid(row=6, column=1)
txtProgress.insert(0,'Waiting')
mainloop()
助けてくれてありがとう。