PYQT 将函数从一个 class 传递到另一个 window

PYQT pass a function from one class to another window

大家好,我想在另一个 window 中使用我的函数在一个 类 函数中获取的目录。我想将选择的目录传递给弹出窗口 window,以便它可以显示所有文件。任何帮助将不胜感激

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

在您的代码中,您没有将返回值放入变量中,您刚刚初始化了 createedditConvertorpage class 的对象 directoryFile 并调用了 selectFilecsvtoxml class.

的函数

更改代码:

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。

请检查这是否解决了您的问题

PYQT 在获得正确的路径方面非常挑剔,而且您经常需要拐弯抹角的代码。这看起来很乱,但这是答案

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)