PyQt5 ile notepad uygulaması yaptım. Küçük bir sorum olacak?

from PyQt5.QtWidgets import QWidget,QApplication,QTextEdit,QHBoxLayout,QVBoxLayout,QFormLayout,QFileDialog,QColorDialog,QPushButton,QLabel,QDialog,QFontDialog,QMessageBox
import sys
import os

class notepad(QWidget):
    def __init__(self):
        super().__init__()
        self.ust_ayarlar()
        self.setUI()

    def setUI(self):

        self.text_edit = QTextEdit(self)
        #self.text_edit.setFont(QFont="Helvetica")

        temizle_butonu = QPushButton("Temizle",self)
        temizle_butonu.clicked.connect(self.temizle)

        ac_butonu = QPushButton("Aç",self)
        ac_butonu.clicked.connect(self.ac)

        kaydet_butonu = QPushButton("Kaydet",self)
        kaydet_butonu.clicked.connect(self.kaydet)

        font_butonu = QPushButton("Font",self)
        font_butonu.clicked.connect(self.font_degistir)

        tema_butonu = QPushButton("Tema",self)
        tema_butonu.clicked.connect(self.tema_dialog)

        self.dosya_kapat_butonu = QPushButton("Kapat",self)
        self.dosya_kapat_butonu.setEnabled(False)
        self.dosya_kapat_butonu.clicked.connect(self.dosya_kapat)


        h_box = QHBoxLayout()
        h_box.addWidget(temizle_butonu)
        h_box.addWidget(ac_butonu)
        h_box.addWidget(kaydet_butonu)

        h_box2 = QHBoxLayout()
        h_box2.addWidget(font_butonu)
        h_box2.addWidget(tema_butonu)

        v_box = QVBoxLayout(self)
        v_box.addLayout(h_box2)
        v_box.addWidget(self.text_edit)
        v_box.addLayout(h_box)
        v_box.addWidget(self.dosya_kapat_butonu)


    def tema_dialog(self):
        self.dialog = QDialog()
        self.dialog.setWindowTitle("Tema Ayarla")

        arka_label = QLabel("Arka Plan: ",self.dialog)
        self.arkarenk_label = QLabel("",self.dialog)
        ön_label = QLabel("Ön Plan: ",self.dialog)
        self.önrenk_label = QLabel("",self.dialog)
        gözAt_butonu = QPushButton("Göz At",self.dialog)
        gözAt_butonu.clicked.connect(self.ayarla)
        self.tamam_butonu = QPushButton("Tamam",self.dialog)
        self.tamam_butonu.clicked.connect(self.tamam)
        self.tamam_butonu.setEnabled(False)

        form = QFormLayout(self.dialog)
        form.addRow(arka_label,self.arkarenk_label)
        form.addRow(ön_label,self.önrenk_label)
        form.addRow(gözAt_butonu,self.tamam_butonu)


        self.dialog.exec()
    def ayarla(self):

        arkaDialog = QColorDialog(self)
        arkaRenk = arkaDialog.getColor(self.text_edit.textBackgroundColor())
        self.arkaRenk = arkaRenk.name()
        self.arkarenk_label.setText(str(self.arkaRenk))
        arkaDialog.close()

        önDialog = QColorDialog(self)
        önRenk = önDialog.getColor(self.text_edit.textBackgroundColor())
        self.önRenk = önRenk.name()
        self.önrenk_label.setText(str(self.önRenk))
        self.tamam_butonu.setEnabled(True)


    def tamam(self):
        try:
            self.text_edit.setStyleSheet("background-color: " + self.arkaRenk + ";color: " + self.önRenk)
            self.dialog.close()

        except Exception:
            QMessageBox.warning(self,"Uyarı","İki Renk Seçmediniz!")

    def font_degistir(self):
        font,durum = QFontDialog.getFont()
        if durum:
            self.text_edit.setFont(font)

    def temizle(self):
        self.text_edit.clear()

    def kaydet(self):
        save_file = QFileDialog().getSaveFileName(self,"Dosya Kaydet",os.getenv("HOME"),"Text File(*.txt)")
        with open(save_file[0],"w") as file:
            file.write(self.text_edit.toPlainText())


    def ac(self):
        self.file_dialog = QFileDialog(self)
        self.open_file = self.file_dialog.getOpenFileName(self,"Dosya Aç",os.getenv("HOME"),"Text File(*.txt)")
        print(self.open_file)
        self.dosya_kapat_butonu.setEnabled(True)
        if self.open_file[0]:
            f = open(self.open_file[0],"r")

            with f:
                textfile = f.read()
                self.text_edit.setText(textfile)


    def dosya_kapat(self):
        pass

    """dosya_kapat() fonksiyonu ile son açılan dosyayı değişiklik yapılmamış ise kapatmak,
        yapılmış ise direkt üzerine yazılmasını istiyorum?"""


    def ust_ayarlar(self):
        self.setWindowTitle("Notepad App")


app = QApplication(sys.argv)
window = notepad()
window.show()
app.exec()

1.Sorum: dosya_kapat() fonksiyonu ile yani kapat butonu ile son açılan dosyayı değişiklik yapılmamış ise kapatmak, yapılmış ise direkt üzerine yazılmasını istiyorum. Nasıl yapabilirim?

2.Sorum: Bir txt dosyamız için renk ve fontu ayarladık mesela. Daha sonra kaydediyoruz. Masaüstünde o txt dosyasını açınca ayarladığımız renk ve font kayboluyor. Sadece yazı kalıyor.
Renk ve font ayarları kaybolmadan programımız dışında o txt dosyasını nasıl açabiliriz?

En basit yöntem dosya içeriği ile TextEdit içeriğini karşılaştırmak olacaktır. Eğer aynı değilse kayıt yapılabilir.


Bilmem, not defterinin böyle bir özelliği var mı? Daha doğrusu .txt formatının böyle bir özelliği var mı?

Ama kendi programımla her txt (aslında türü önemsiz) dosyasını önceden ayarlandığı şekilde görüntülemek istiyorum diyorsanız JSON dosyaları kullanabilirsiniz.

Cevabınız için teşekkürler