2016-08-28 49 views
0

我有N文件打開,我想獲得N新文件。我想通過使用線程來處理一個文件,以防止GUI tkinter凍結。在處理文件的實際情況下,我需要等待大約1-2分鐘。如何通過使用線程一次讀取和寫入一個文件

open file1-->read-->create new file1-->close file 
open file2-->read-->create new file2-->close file 
open file3-->read-->create new file3-->close file 

在下面的測試代碼中,在讀寫模式下沒有打開文件,我使用了一個簡單的列表。在while循環如果計數是一個很大的數字,我沒有正確的結果:

#I expect an output like: 
#100000000 file1 test release 
#100000000 file2 test release 
#100000000 file3 test release 
#Indeed I obtain strange result like file3 as first result not in third position 

from tkinter import filedialog 
from tkinter import * 
from tkinter import ttk 
import threading, time 

def th(lol): 
     global mythread, lock 
     mythread = threading.Thread(target=aprifil2, args=(lol,)) 
     mythread.start() 

def aprifil2(lol): 
       global lock, m 
       lock.acquire() 
       try: 
        s=0 
        while(s<100000000): 
         s+=1 
        print(s, i) 
        scr() 
       finally: 
         print('release\n') 
         try: 
          lock.release() 
         except RuntimeError: 
              print('end') 

def scr(): 
      print('test\n') 

def ok(): 
      global lock, i 
      lock = threading.Lock() 
      m=['file1','file2', 'file3'] 
      for i in m: 
        lol=i 
        th(lol) 
        time.sleep(1) 

finestra= Tk() 
button_cerca = ttk.Button(finestra, text = "avvia", command = ok) 
button_cerca.pack() 
button_a = Entry(finestra) 
button_a.pack() 
+0

你的意思是你想要的3個文件被依次處理?在這種情況下,您必須創建1個線程並處理其中的3個文件,而不是3個線程。 –

+0

是,依次。一個線程是enoght,但我需要tkinter GUI不凍結...無論如何,我不明白我的錯誤。 – lausent

回答

0

問題解決了:

from tkinter import * 
from tkinter import ttk 
from datetime import datetime as dt 
import threading, time 


def th(): 
     global mythread, cro 
     mythread = threading.Thread(target=ok) 
     cro = threading.Thread(target=cronou) 
     mythread.start() 

def aprifil2(z): 
         global m, state_crono, zero 
         s=0 
         while(s<10000000): 
           s+=1 
         print(s, z) 
         scr() 
         print(crono.get()) 
         print('\n') 
         zero = dt.now()-dt.now() 
         if(z==m[len(m)-1]): 
             state_crono = False 
             time.sleep(0.01) 
             crono.set('') 

def scr(): 
      print('test\n') 

def ok(): 
      global i, m, state_crono 
      state_crono = True 
      cro.start() 
      m=['file1','file2', 'file3'] 
      for z in m: 
        aprifil2(z) 

def cronou(): 
      global zero 
      zero = dt.now()-dt.now() 
      while(state_crono == True): 
             ti = dt.now() 
             time.sleep(0.01) 
             tf = dt.now() 
             zero += tf-ti 
             crono.set(str(zero)[0:10]) 

finestra= Tk() 
button_cerca = ttk.Button(finestra, text = "avvia", command = th) 
button_cerca.pack() 
button_a = Entry(finestra) 
button_a.pack() 
crono = StringVar() 
tex_cronometro = Label(finestra, textvariable=crono, font=("Helvetica",11,"bold")) 
tex_cronometro.pack() 
finestra.mainloop() 
+0

這段代碼很難理解,並且可能不會對別人有所幫助。最好添加一個解釋。例如,while(s <10000000)'循環有什麼意義?如果你使用了有意義的變量名稱,那麼它也會有所幫助,'z'和'm'不會暗示它們代表什麼。 –

+0

while循環表示寫入過程(新文件)。 「z」表示每個必須依次打開的文件。所以我打開一個文件(如z [0],z [1]等等)並寫入。 – lausent

相關問題