이 프로젝트에 최종 수정 만하십시오. 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()
감사합니다.
들여 쓰기를 수정하고 수동 json 대신'import json'을 사용하십시오. 또한 전역 변수를 건드려서는 안되며, 파이썬으로 몇 년 동안 코딩하지 않았습니다. – dav1d
들여 쓰기에 어떤 문제가 있는지 잘 모르겠습니다. json 모듈이 있다는 것을 몰랐습니다. 나는 일을 끝내기 위해 무엇이든 살아 있도록 코드를 작성하지 않습니다. –