Encoding problem on 'utf-8' kv file on Windows
See original GitHub issueI’m developing a multi-platform app for Linux and Windows 7. All my files were first written on Linux and encoded as utf-8
, but when I open this same project on Windows the kv
file is read using the cp1252
encoding. The same thing does not seems to happen to my .py
files maybe because I’m using python3.
As a consequence the Unicode characters written on the kv
file won’t render correctly on the Kivy app. The string 'Título'
will show as TÃ-tulo
.
My settings are: Kivy=1.9.1, Python=3.4.4, Windows 7 x64 Home Premium.
Also my python was installed using Anaconda, but this is probably unrelated.
To reproduce the problem:
Write a kv
file encoded with utf-8
:
# test.kv
<myButton@Button>:
text: 'Título'
On python interpreter or .py
script:
import kivy
from kivy.lang import Builder
from kivy.uix.button import Button
Builder.load_file('test.kv')
class myButton(Button):
pass
print( myButton().text == 'Título' ) # False
print( myButton().text.encode('cp1252').decode() == 'Título' ) # True
The multi-platform workaround I found was this:
# test.kv
<myButton@Button>:
text: str(b'T\xc3\xadtulo'.decode())
Issue Analytics
- State:
- Created 8 years ago
- Reactions:4
- Comments:27 (5 by maintainers)
Top Results From Across the Web
Kivy Encoding Japanese (Unicode) - python - Stack Overflow
So the problem was in how the file was saved. I got it working in .py file with Builder.load_string, however converting .kv still...
Read more >Kivy+python3 support on Windows for Multi-byte (Japanese ...
A UTF-8 KV file in Windows sometimes fails to display properly, ... and/or making an encoding conversion for all KV files necessary.
Read more >File encoding problem - MSDN - Microsoft
I reset the settings, created a new file, and the new file has Unicode (UTF-8 with signature) - Codepage 65001 selected. But when...
Read more >Unable to change encoding to UTF-8 in Open Office [closed]
The problem seems to be in encoding. I am not sure which encohing OO uses by default. I can see the documents in...
Read more >Resolving Issue with Logging Formatter & Unicode in Python 2.7
In Python 2.7 on Windows, I was using a RotatingFileHandler with a ... even when I was opening the file with the UTF-8...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yes @ChristianTremblay, this is only a Windows bug. This is actually because windows default encoding is cp1252, causing Kivy to read the .kv file as if encoded that way. Maybe the solution proposed by @KeyWeeUsr really helps, I haven’t tried that, but might be cleaner than the other workaround proposed.
I agree, a good solution would be to to allow encode specification on the .kv file like the way we can do in python:
Ok, I know this is not ideal but if none of these options are working for you, you can set your desired string to a variable in the main app class in your .py file, which has utf-8 encoding, and then access it in your .kv file.
#.py
class MainApp(App):
struser = ('Nome de usuário')
#.kv
Label:
text: app.struser
worked for me