2016-08-28 50 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

相关问题