Hi,

you should use the unicode() function instead of str().

Detlev

On Samstag, 5. Mai 2007, piotr maliński wrote:
> Hi, I'm writing a simple text editor in PyQT4. It works for ASCII
> characters but I use UTF-8 encoded files with non-ASCI characters like
> Polish letters (łóżźć etc.) I've used the "simple" way:
>
> ###########################
> import sys
> from PyQt4 import QtCore, QtGui
> from edytor import Ui_notatnik
>
> class StartQT4(QtGui.QMainWindow):
>       def __init__(self, parent=None):
>               QtGui.QWidget.__init__(self, parent)
>               self.ui = Ui_notatnik()
>               self.ui.setupUi(self)
>               
> QtCore.QObject.connect(self.ui.button_open,QtCore.SIGNAL("clicked()"),
> self.file_dialog)
>               
> QtCore.QObject.connect(self.ui.button_save,QtCore.SIGNAL("clicked()"),
> self.file_save)
>       def file_dialog(self):
>               fd = QtGui.QFileDialog(self)
>               self.filename = fd.getOpenFileName()
>               from os.path import isfile
>               if isfile(self.filename):
>                       text = open(self.filename).read()
>                       self.ui.editor_window.setText(text)
>       def file_save(self):
>               from os.path import isfile
>               if isfile(self.filename):
>                       file = open(self.filename, 'w')
>                       file.write(self.ui.editor_window.toPlainText())
>                       file.close()
>
> if __name__ == "__main__":
>       app = QtGui.QApplication(sys.argv)
>       myapp = StartQT4()
>       myapp.show()
>       sys.exit(app.exec_())
> ##############################
> It works ok for ASCI characters but when I open a file with Polish
> letters they aren't displayed properly (textEdit) - also saving a file
> with such letters won't save them correctly.
>
> Similar with QFile:
> ##############################
> # -*- coding: utf-8 -*-
> import sys
> from PyQt4 import QtCore, QtGui
> from edytor import Ui_notatnik
>
> class StartQT4(QtGui.QMainWindow):
>       def __init__(self, parent=None):
>               QtGui.QWidget.__init__(self, parent)
>               self.ui = Ui_notatnik()
>               self.ui.setupUi(self)
>               
> QtCore.QObject.connect(self.ui.button_open,QtCore.SIGNAL("clicked()"),
> self.file_dialog)
>               
> QtCore.QObject.connect(self.ui.button_save,QtCore.SIGNAL("clicked()"),
> self.file_save)
>       def file_dialog(self):
>               fd = QtGui.QFileDialog(self)
>               self.filename = fd.getOpenFileName()
>               from os.path import isfile
>               if isfile(self.filename):
>                       s = QtCore.QFile(str(self.filename))
>                       s.open(QtCore.QIODevice.ReadOnly)
>                       self.ui.editor_window.setPlainText(str(s.readAll()))
>                       s.close()
>       def file_save(self):
>               from os.path import isfile
>               if isfile(self.filename):
>                       s = QtCore.QFile(str(self.filename))
>                       s.open(QtCore.QIODevice.WriteOnly)
>                       s.writeData(str(self.ui.editor_window.toPlainText()))
>                       s.close()
>
> if __name__ == "__main__":
>       app = QtGui.QApplication(sys.argv)
>       myapp = StartQT4()
>       myapp.show()
>       sys.exit(app.exec_())
> #######################################
> but when I try to save the file with Polish letters I get:
>
> UnicodeEncodeError: 'ascii' codec can't encode characters in position
> 20-23: ordinal not in range(128)
>
> _______________________________________________
> PyQt mailing list    [email protected]
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt



-- 
Detlev Offenbach
[EMAIL PROTECTED]

_______________________________________________
PyQt mailing list    [email protected]
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

Reply via email to