2012-01-12 59 views
0

我有一个小型的python/pyqt程序,它在第一次运行时运行良好。但是如果我关闭并快速重启(大约3秒),这个定义会给出错误的结果。它运行正常,没有错误,但如果oktype将不会返回true,即使它与第一次运行它完全相同的文件!这个代码有什么用?或者是在我的代码中更高,或者可能是某种垃圾收集错误?关闭并快速启动Python程序的问题

def fileChanged(fileIn, pubNotes, fileType): 
    pubNotes.clear() 
    fileType.clear() 

    filename = str(fileIn.text()) 

    foundtype = 0 
    oktype = '' 
    okfiles = ('.max','.ma','.mb','.ni','.nk', '.psd','.ztl', '.tif', '.tiff') 
    for ftype in okfiles: 
     if filename.endswith(ftype): 
      print('File: %s matches pattern %s') % (filename,ftype) 
      foundtype = 1 
      print 'ftype er %s' % ftype 
      oktype = ftype 
      break 

    if foundtype: 
     print('File is of type %s') % oktype 
     if oktype is '.psd': 
      typeopt = ['Matte Paint','Texture paint'] 
      for i in range(len(typeopt)): 
       fileType.addItem(typeopt) 
     if oktype is '.nk': 
      typeopt = ['Comp', 'Precomp', 'Roto'] 
      for i in range(len(typeopt)): 
       fileType.addItem(typeopt[i]) 
     if oktype is '.max': 
      typeopt = ['Model'] 
      for i in range(len(typeopt)): 
       fileType.addItem(typeopt[i]) 
     if oktype is '.tif': 
      typeopt = ['Texture'] 
      for i in range(len(typeopt)): 
       fileType.addItem(typeopt[i]) 
      #update comboBox_types here 
    else: 
     messageBox('File is not of known type, please drag other file') 
     fileIn.clear 
+4

您是否尝试过使用'oktype =='而不是'oktype is'? – TyrantWave 2012-01-12 16:11:50

+0

与垃圾回收无关 - 问题几乎可以肯定是实际的文件句柄,如果它没有正确关闭或者需要由操作系统清理。 – 2012-01-12 16:14:46

+1

TyrantWave:字符串比较没有区别。 – 2012-01-12 16:15:58

回答

0

是的! @TyrantWave和@reclosedev你是绝对正确的。我转而使用==而不是is,现在它就像一个魅力!非常感谢!