G'day All,
我有一个小应用程序,os.walks收集一些数据。虽然它正在工作,但我认为将一个包含文本“Processing”的Entry小部件放入其中并在步行完成时将其更改为“已完成”可能会很好。
问题是“处理”永远不会出现。处理需要花费很多秒才能看到它不会太快。
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
如果它不起作用,那不是火车粉碎,但我不知道为什么文字没有出现。
一如既往,感谢所有建议。
答案 0 :(得分:1)
简而言之,您永远不会让您的程序有机会显示它。重绘屏幕仅作为重绘事件的结果发生,因此除非事件循环有机会为该事件提供服务,否则不会显示任何内容。这不是Tkinter独有的 - 所有GUI工具包都以这种方式工作。
简单的解决方案是在您希望更新屏幕时调用txtProgress.update_idletasks()
。这将允许刷新屏幕的事件运行。这不是最好的解决方案,但它可以解决您当前的问题。
最好的解决方案是重构您的程序,以便在单独的线程或进程中执行长时间运行的工作,或者将工作分解为可以在事件循环的每次迭代中一次完成一个的块。
答案 1 :(得分:0)
只是对这个项目进行最后的修改。我应用了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()
感谢帮助人员。