2015-10-16 198 views
0

嘿傢伙我想使用我的函數在另一個窗口中的一個類的函數中獲取的目錄。我想將選擇的目錄傳遞給彈出窗口,以便顯示所有文件。任何幫助將apprciatedPYQT將功能從一個類傳遞到另一個窗口

class createedditConvertorpage(QtGui.QMainWindow): 
     def __init__(self,parent = None): 
      QtGui.QWidget.__init__(self, parent) 


    def selectFilecsvtoxml(self): 

      directory = QtGui.QFileDialog.getExistingDirectory(self, "Pick a folder") 
      print directory 
      self.listDirPath.setText(directory) 

      for file_name in os.listdir(directory): 
       if not file_name.startswith("."): 

        print (file_name) + " this is selectFilcestoxml" 
      self.directory = directory 
      return directory 

class readoutWindow(QtGui.QDialog): 
    def openTxt(self): 
     directoryFile = createedditConvertorpage() 
     directoryFile.selectFilecsvtoxml() 
     print "this s open text" 
     print str(directoryFile) 
     for file_name in directoryFile: 
      if file_name.endswith(".txt"): 


       print (file_name) + " this is txt file" 

    File "/home/ed/Development/Python/Workmain/windows.py", line 1425, in home 
    self.openTxt() 
    File "/home/ed/Development/Python/Workmain/windows.py", line 1442, in openTxt 
    for file_name in directoryFile: 
TypeError: 'createedditConvertorpage' object is not iterable 

回答

2

在你的代碼不採取返回值到一個變量,你剛纔初始化對象directoryFilecreateedditConvertorpage類並從該類叫你selectFilecsvtoxml功能。

改變的代碼:

class createedditConvertorpage(QtGui.QMainWindow): 
    def __init__(self,parent = None): 
     QtGui.QWidget.__init__(self, parent) 


def selectFilecsvtoxml(self): 

     directory = QtGui.QFileDialog.getExistingDirectory(self, "Pick a folder") 
     print directory 
     self.listDirPath.setText(directory) 

     for file_name in os.listdir(directory): 
      if not file_name.startswith("."): 

       print (file_name) + " this is selectFilcestoxml" 
     self.directory = directory 
     return directory 

class readoutWindow(QtGui.QDialog): 
def openTxt(self): 
    directoryFile = createedditConvertorpage() 
    dir1=directoryFile.selectFilecsvtoxml() 
    print "this s open text" 
    print str(dir1) 
    for file_name in dir1: 
     if file_name.endswith(".txt"): 
      print (file_name) + " this is txt file" 

我已經指派返回目錄變量DIR1。

請檢查該解決您的問題

+0

你的回答很近,給你信用 – Anekdotin

0

PyQt的是關於讓正確的路徑,往往你必須柺杖代碼非常finnicky。這看起來很凌亂,但這裏是答案

def openTxt(self): 
    directoryFile = createedditConvertorpage() 
    dir1=directoryFile.selectFilecsvtoxml() 
    print "this s open text" 
    print str(dir1) + "this is directorry of opentxt" 
    os.chdir(dir1) 
    print os.getcwd()+ " this is directory before looking for txt" 
    files = [f for f in os.listdir('.') if os.path.isfile(f)] 
    for file_name in files: 

     if file_name.endswith(".txt"): 
      print dir1 + "/" + (file_name) + " this is txt file" 
      readMe = open(file_name,'r').read() 
      self.textEdit.setText(readMe)