TypeError:强制转换为Unicode:需要字符串或缓冲区,bool发现...收到错误?

时间:2012-07-26 21:02:54

标签: pyqt

在我的代码中读取文件名时,我一直收到“TypeError:coercing to Unicode:need string or buffer,bool found”。我似乎无法弄明白,因为我明确地使用filename = str(“QFileDialog .....”)......我哪里出错?

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

1 个答案:

答案 0 :(得分:2)

问题的根源在于:

openAction.triggered.connect(self.openNewFile)
openAction.triggered.connect(self.setDataFile)

您是否真的想将openActionsetDataFile连接起来?

请注意triggered信号的签名是:

void QAction::triggered ( bool checked = false ) [signal]

这意味着setDataFile方法会在其filename参数设置为False的情况下继续调用 - 并解释为什么您看到TypeError抱怨接收布尔值而不是一串。