當我在我的代碼中讀取文件名時,我一直收到「TypeError:強制爲Unicode:需要字符串或緩衝區,發現布爾」。我似乎無法弄清楚,因爲我明確使用filename = str(「QFileDialog .....」)...我哪裏錯了?TypeError:強制爲Unicode:需要字符串或緩衝區,找到bool ...收到了錯誤?
class MainWindow(QMainWindow):
def __init__(self, filename=None, parent=None):
super(MainWindow,self).__init__(parent)
self.fileheader=None
self.frameheader = None
self.initUI()
self.resize(900,900)
if filename:
self.setDataFile(filename)
def initUI(self):
self.filetable=QTableWidget()
self.frametable=QTableWidget()
self.filetable.setEditTriggers(self.filetable.NoEditTriggers)
self.frametable.setEditTriggers(self.filetable.NoEditTriggers)
self.imageBrowser=ImageViewer()
self.imagesplitter=QSplitter(Qt.Horizontal)
self.tablesplitter=QSplitter(Qt.Horizontal)
self.imagesplitter.addWidget(self.imageBrowser)
self.tablesplitter.addWidget(self.imagesplitter)
self.tablesplitter.addWidget(self.filetable)
self.tablesplitter.addWidget(self.frametable)
self.setCentralWidget(self.tablesplitter)
self._setTables()
self._createMenuBar()
self.imageBrowser.slider.sliderMoved.connect(self.change_image_index)
def _setTables(self):
self.filetable.setRowCount(len(FileHeader.fileheader_fields))
self.filetable.setColumnCount(2)
self.filetable.setHorizontalHeaderLabels(['File Header','value'])
for i,field in enumerate(FileHeader.fileheader_fields):
# just set the field names...no data
name=QTableWidgetItem(field)
#value=QTableWidgetItem(unicode(getattr(self.fileheader,field)))
self.filetable.setItem(i,0,name)
#self.filetable.setItem(i,1,value)
#self.frameheader=self.fileheader.frameAtIndex(0)
self.frametable.setRowCount(len(Frame.frameheader_fields))
self.frametable.setColumnCount(2)
self.frametable.setHorizontalHeaderLabels(['Frame Header','Value'])
for i,fields in enumerate(Frame.frameheader_fields):
# name only...no data yet
Name=QTableWidgetItem(fields)
#Value=QTableWidgetItem(unicode(getattr(self.frameheader,fields)))
self.frametable.setItem(i,0,Name)
def _createMenuBar(self):
menubar=self.menuBar()
exitAction=QAction(QIcon('exit.png'),'&Exit',self)
exitAction.setShortcut('Ctrl+Q')
exitAction.triggered.connect(qApp.quit)
openAction=QAction(QIcon('open.png'),'&Open',self)
openAction.setShortcut('Ctrl+O')
openAction.triggered.connect(self.openNewFile)
openAction.triggered.connect(self.setDataFile)
fileMenu=menubar.addMenu('&File')
fileMenu.addAction(openAction)
fileMenu.addAction(exitAction)
def populate(self):
if not self.frameheader:
print "[DEBUG] populate(): self.frameheader is None"
return
for i,field in enumerate(FileHeader.fileheader_fields):
name=QTableWidgetItem(field)
value=QTableWidgetItem(unicode(getattr(self.fileheader,field)))
self.filetable.setItem(i,0,name)
self.filetable.setItem(i,1,value)
#self.frametable.setRowCount(len(self.frameheader.frameheader_fields))
#self.frametable.setColumnCount(2)
#self.frametable.setHorizontalHeaderLabels(['Frame Header','Value'])
for i,fields in enumerate(self.frameheader.frameheader_fields):
Name=QTableWidgetItem(fields)
Value=QTableWidgetItem(unicode(getattr(self.frameheader,fields)))
self.frametable.setItem(i,0,Name)
self.frametable.setItem(i,1,Value)
def change_image_index(self,val):
if not self.fileheader:
print "[DEBUG] change_image_index(): self.fileheader is None"
return
self.frameheader=self.fileheader.frameAtIndex(val)
self.populate()
def setDataFile(self, filename):
self.fileheader=FileHeader(filename)
self.imageBrowser.setFileHeader(self.fileheader)
#frame=Frame(filename) # you only need the frame on demand
self.change_image_index(0)
def openNewFile(self):
filename=str(QFileDialog.getOpenFileName(None,"open file","C:/vprice/DIDSON/DIDSON Data","*.ddf"))
self.setDataFile(filename)
if __name__=="__main__":
app=QApplication([])
w=MainWindow()
w.show()
w.raise_()
app.exec_()
和錯誤代碼:
Traceback (most recent call last):
File "C:\Users\Tory\Desktop\PYTHON NOAA\DIDSONGUIqwttest.py", line 197, in setDataFile
self.fileheader=FileHeader(filename)
File "C:\Python27\lib\fileheader.py", line 31, in __init__
self.infile=open(filename,'rb')
TypeError: coercing to Unicode: need string or buffer, bool found
我應該補充一點,我可以在沒有使用IDLE的情況下運行這個腳本......我目前正在嘗試使用guiqwt和python(x,y)來運行它。 – 2012-07-26 21:10:43
你能打印'filename'並在這裏粘貼輸出,所以我們知道你傳遞給fileheader – Achayan 2012-07-28 03:43:12
它給了我真正的文件名:C:/ vprice/DIDSON/DIDSON Data/2011-08-09_150715_SyncOut_HF.ddf – 2012-08-01 13:08:04