嘿伙计我想在另一个窗口中使用我的函数在一个类函数中获取的目录。我想将选择的目录传递给弹出窗口,以便它可以显示所有文件。任何帮助都将被适用
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
答案 0 :(得分:2)
在您的代码中,您没有将返回的值转换为变量,您刚刚初始化了directoryFile
类的对象createedditConvertorpage
,并从该类调用了selectFilecsvtoxml
函数。 / p>
更改代码:
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。
请检查这是否可以解决您的问题
答案 1 :(得分:0)
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)