ファイル分析の進行状況をいくつかの独立したルーチンで順番に表示するには、各分析ルーチンに時間がかかることがあります。添付のデモコードは、私が持っていた問題を示しています。問題は、分析が終了した後にディスプレイが更新されるだけであるということですか?なぜコードが何を意図したものではなく、それを修正する方法を知っていることを感謝します。注:routine1 & 2は2つの別々の.pyファイルにあります。Tkinterのテキストが順番に表示されない
from Tkinter import *
import tkFileDialog
import tkMessageBox
import routine1
import routine2
import sys
class Analysis(Frame):
def __init__(self):
Frame.__init__(self)
self.text = Text(self, height=20, width=60) # 20 characters
self.pack()
scroll=Scrollbar(self)
scroll.pack(side=RIGHT, fill=Y)
scroll.config(command=self.text.yview)
self.text.config(background='white')
self.text.pack(expand=YES, fill=BOTH)
def selectfile(self):
fname = tkFileDialog.askopenfilename()
self.text.delete(1.0, END)
self.text.insert(INSERT, ' working on routine 1: \n')
routine1.main(fname)
self.text.insert(INSERT, ' routine 1 done: \n')
self.text.insert(INSERT, ' working on routine 2: \n')
routine2.main(fname)
self.text.insert(INSERT, ' routine 2 done: ')
sys.exit()
def main():
tk = Tk()
tk.title('Data Analysis')
atext = Analysis()
atext.pack()
open_button = Button(tk, text="Select Data",
activeforeground='blue', command=atext.selectfile)
open_button.pack()
message='''
Select file to be analysized
'''
atext.text.insert(END,message)
tk.mainloop()
if __name__ == "__main__":
main()
routine1.py
import time
def main(Filename):
print Filename
time.sleep(1) # to simulate process time
return
routine2.py
import time
def main(Filename):
print Filename
time.sleep(1) # to simulate process time
return
Tkinterウィンドウは、実行している関数がすべて終了した後、コントロールがメインループに戻るときにのみ更新されます。 'sleep 'を呼び出さなくてもウィンドウを更新する機会は与えられません。ルーチンを非同期で実行するには、スレッドを使用します。 – Kevin
@kevin:いいえ。 – martineau