简体   繁体   English

PYQT将函数从一个类传递到另一个窗口

[英]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

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. 在您的代码中,您没有将返回的值带入变量中,您只是初始化了createedditConvertorpage类的对象directoryFile ,并从该类调用了selectFilecsvtoxml函数。

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. 我已将返回的目录分配给变量dir1。

Please check if this fixes your problem 请检查是否可以解决您的问题

PYQT is very finnicky about getting the correct path and often you have to crutch code. PYQT对于获得正确的路径非常挑剔,并且常常需要拐弯抹角的代码。 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM