0
基本的なテキストエディタを作成しようとしていますが、PYQT5を使用してファイルを開くか保存しようとしたときにエラーが発生しました。オープン:PyQT5でファイルを開くときに無効なファイルエラーが発生しました
File "D:\NicKLz\Documents\GitHub\MiscProjects\TextEditor\txtEditor.py", line 81, in open
with open(self.filename, "r") as file:
TypeError: invalid file: ('D:/NicKLz/Documents/GitHub/MiscProjects/TextEditor/Hello.writer', 'Files (*.*)')
そして、私が取得保存して:ここで
File "D:\NicKLz\Documents\GitHub\MiscProjects\TextEditor\txtEditor.py", line 89, in save
if not self.filename.endswith(".writer"):
AttributeError: 'tuple' object has no attribute 'endswith'
は、私はそれらの機能のために持っているものです。
def open(self):
#Get filename and show only .txt files
self.filename = QtWidgets.QFileDialog.getOpenFileName(self, 'Open File', ".", "Files (*.*)")
if self.filename:
with open(self.filename, "r") as file:
self.text.setText(file.read())
def save(self):
#Only Open this dialog if there is no filename yet
if not self.filename:
self.filename = QtWidgets.QFileDialog.getSaveFileName(self, "Save File")
#Add the appropriate extension if not currently in place
if not self.filename.endswith(".writer"):
self.filename += ".writer"
#Store Contents and format in html format which QT does a nice job of implementing for us already
with open(self.filename,"w") as file:
file.write(self.text.toHtml())
すべてのヘルプは次のようになり本当に感謝。
うわー本当にシンプルで完璧に働いて、助けてくれてありがとう!!!! –