Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

pyqt5 file_open method error, i got this error when i opened an image

 def open_file(self):
    name=QFileDialog.getOpenFileName(self, 'Open File')
    file=open(name, 'rb', encoding= 'utf8')

    self.editor()

    with file:
        text=file.read()
        self.textEdit.setText(text)

expected str, bytes or os.PathLike object, not tuple i got this error when i opened an image.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

getOpenFileName return a tuple with the name and the extension

def open_file(self):
    name = QFileDialog.getOpenFileName(self, 'Open File')
    file = open(name[0], 'rb', encoding='utf8')

    self.editor()

    with file:
        text = file.read()
        self.textEdit.setText(text)

You should also make a test to see if you return something otherwise the program could crash

def open_file(self):
    name = QFileDialog.getOpenFileName(self, 'Open File')
    if name[0] == '':
        return
    file = open(name[0], 'rb', encoding='utf8')

    self.editor()

    with file:
        text = file.read()
        self.textEdit.setText(text)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading