How to configure Portuguese in kivy? [closed]

0

My App has two files, * .py and * .kv, but I'm having problems with accents in my GUI. I have already tried to resolve it in the .py file with

  

#encoding: -*- utf-8 -*-

But the problem continues, please help me. Settings: Python 3.6, Kivy 1.10.1.

    
asked by anonymous 08.03.2018 / 21:47

1 answer

1

I've done some testing here, including using a .kv file, and everything works perfectly. but I'm on a Unix-like system and you're probably on Windows.

I assume you have certified that your files - either .py source code or .kv files are actually using utf-8 as encoding. Otherwise, configure your programming editor to use utf-8.

If this is not the case, Kivy is trying to open the file in the native system coding, which is Python - and in the case of Windows, it is not "utf-8". A quick workaround would be to write your .kv file to native (latin1) encoding, but this would mean that operating systems using modern character sets by default, such as MacOS, Linux, Android, and even a future version of Windows, would have a problem with your app.

Well - I've done some searches, and you can see that this is probably the case too: link

The solution is, instead of letting Kivy load your .kv file automatically, use the "Builder.load_string" and read the file manually, passing the encoding explicitly.

To do this, instead of having the .kv file name equal to the name of your "App" class, as it is necessary for kivy to load the file automatically, put any other name in the .kv file, and in the .build method of your app, do:

from kivy.lang.builder import Builder

class MyApp(App):
    def build(self):
        Builder.load_string(open("meu_arquivo.kv", encoding="utf-8").read(), rulesonly=True)
        return MyMainWidget()

Note that using "load_string", the code that reads the .kv file is now under your control, so just use the open of Python, forcing text encoding to utf-8. >     

08.03.2018 / 23:29