The following code:
self.texteditor = QTextEdit ('')
def openmenu ():
filename = QFileDialog.getOpenFileName (self, 'open file', '', 'text files (* .txt)')
filename = os.path.abspath (filename [0])
program settings.path openfile = filename
file content = '' .join (open (file name, encoding = "utf8"). readlines ())
self.texteditor.setText (file content)
When testing the function I got the following with a text file Error message: Process finished with exit code -1073740791 (0xC0000409)
Question: What can I do against it?
I have recreated what you want with the code you gave and got this to work
import sys
import os
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class Wnd(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.initUI()
self.menuoffnen()
pass
def initUI(self):
self.setGeometry(200, 200, 800, 600)
self.layout = QVBoxLayout()
self.layout.setContentsMargins(0, 0, 0, 0)
self.setLayout(self.layout)
self.texteditor = QPlainTextEdit('')
font = QFont()
font.setPointSize(12)
self.texteditor.setFont(font)
#self.layout.addWidget(editormenu) # Editormenü
self.setCentralWidget(self.texteditor) # Eingabefeld für Texdateien
self.setWindowTitle("NodeEditor")
self.show()
def menuoffnen(self):
dateiname = QFileDialog.getOpenFileName(self, 'Datei öffnen','','Textdateien (*.txt)')
dateiname = os.path.abspath(dateiname[0])
dateininhalt = ''.join(open(dateiname, encoding="utf-8").readlines())
self.texteditor.setPlainText(dateininhalt)
self.setWindowTitle(os.path.basename(dateiname) + " - Marlems PQTTexteditor")
if __name__ == '__main__':
app = QApplication(sys.argv)
wnd = Wnd()
sys.exit(app.exec_())
I have tried it with a simple txt file and it worked without errors. Could you test it with your file?
I also find that you forgot to pass self as a parameter into menuoffnen, or is it again a problem in the post?
Yes you can put all of this in an try except block you should do that any way when a user can input things. Have you tried to use a different text file? And sometimes with decode errors it helps to leave out the encoding parameter completly.
Here is something about detecting the encoding of a file. It may be usefull if you want to support multiple encodings in the future.
User contributions licensed under CC BY-SA 3.0