1

コードでファイル名を読み取るときに、「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
4

1 に答える 1

2

問題の原因は次のとおりです。

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

openActionも接続するつもりだったのsetDataFileですか?

triggeredシグナルのシグネチャは次のとおりです。

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

これは、引数が - に設定されたsetDataFile状態でメソッドが呼び出され続けることを意味し、文字列ではなくブール値を受け取ることについて不平を言う理由を説明しています。filenameFalseTypeError

于 2012-08-02T02:02:54.180 に答える