[PyQt] Accented characters

alteo_gange romanocaldoni at free.fr
Fri May 2 12:29:50 BST 2008


Le vendredi 02 mai 2008, Christoph Burgmer a écrit :
> If I'm not wrong this "trick" will only work for people who use utf8 as
> default encoding.
>
> You may want to read about Python's way of dealing with encodings and
> Unicode, but that's off topic here.
>
> Little hint:
>
> _, system_encoding = locale.getdefaultlocale()
>
> print unicode(your_string).encode(system_encoding)
>

Thank you Christoph. That settles me an another crack:

I was problems with both "print" and "unicode":

The next script works when it starts with or without terminal (that is file 
navigator):

-------------------------------------------
#!/usr/bin/python
# -*- coding: Utf-8 -*-

import sys, locale
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class Widget(QWidget):
	def __init__(self,parent=None):
		QWidget.__init__(self)
		
		# File selection (the file's name is accented: ex. animé.txt)
		file = QFileDialog.getOpenFileName(self, "Open",QDir.homePath(),"All files 
(*.*)")
		system_encoding = locale.getdefaultlocale()
		print unicode(file).encode(system_encoding[1]) #####
		QMessageBox.information(self, "title", "Print Success!")
		
if __name__ == "__main__":
	app = QApplication(sys.argv)
	main = Widget()
	#main.show()
	sys.exit(app.exec_())
-------------------------------------------

The next script works only when it starts with terminal:

-------------------------------------------
#!/usr/bin/python
# -*- coding: Utf-8 -*-

import sys, locale
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class Widget(QWidget):
	def __init__(self,parent=None):
		QWidget.__init__(self)
		
		# File selection (the file's name is accented: ex. animé.txt)
		file = QFileDialog.getOpenFileName(self, "Open",QDir.homePath(),"All files 
(*.*)")
		print unicode(file) #####
		QMessageBox.information(self, "title", "Print Success!")
		
if __name__ == "__main__":
	app = QApplication(sys.argv)
	main = Widget()
	#main.show()
	sys.exit(app.exec_())
-------------------------------------------

Both "print" and "unicode" are very dangerous.

-- 
alteo_gange




More information about the PyQt mailing list