0

G'day 所有,

我有一个 os.walks 收集一些数据的小应用程序。虽然它正在工作,但我认为在其中放置一个带有文本“正在处理”的条目小部件并在行走完成后将其更改为“已完成”可能会很好。

问题是“处理”永远不会出现。处理需要几秒钟,所以它不会很快结束而无法看到。

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

如果它不起作用,这不是火车粉碎,但我不明白为什么没有出现文本。

一如既往,所有的建议都欣然接受。

4

2 回答 2

1

简而言之,你永远不会给你的程序展示它的机会。重绘屏幕仅作为重绘事件的结果发生,因此除非事件循环有机会为该事件提供服务,否则不会显示任何内容。这不是 Tkinter 独有的——所有 GUI 工具包都以这种方式工作。

txtProgress.update_idletasks()简单的解决方案是在您想要更新屏幕时调用。这将允许刷新屏幕的事件运行。这不是最好的解决方案,但它可能会解决您的直接问题。

最好的解决方案是重构您的程序,以便在单独的线程或进程中执行长时间运行的工作,或者将工作分解为可以在事件循环的每次迭代中一次完成的块。

于 2012-10-14T12:37:18.233 回答
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()

谢谢你们的帮助。

于 2012-10-22T11:17:20.057 回答