Anekdotin
Anekdotin

Reputation: 1591

PYQT pass a function from one class to another window

Hey guys I want to use the directory that my function gets in one classes function in another window. I want to pass the directory chosen to the popup window so it can show all the files. Any help would be apprciated

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

Upvotes: 0

Views: 1064

Answers (2)

Anekdotin
Anekdotin

Reputation: 1591

PYQT is very finnicky about getting the correct path and often you have to crutch code. This looks messy but here is the answer

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)

Upvotes: 0

Pranay Mathur
Pranay Mathur

Reputation: 913

In your Code you are not taking the returned value into a variable, you have just initialised the object directoryFile of your createedditConvertorpage class and called your selectFilecsvtoxml function from that class.

Changed Code:

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"

I have assigned the returned directory to variable dir1.

Please check if this fixes your problem

Upvotes: 2

Related Questions