How to configure Portuguese in kivy?

Asked

Viewed 1,150 times

0

My App has two files, *.py and *.Kv, but I’m having problems with accents in my GUI. I’ve tried to resolve in the . py with file

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

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

  • Please, when asking questions, put enough code snippet to reproduce the problem independently. This prevents the respondent from having to recreate their entire problem, and creates an answer with more context.

  • I tested Kivy’s "hello world" code on https://kivy.org/docs/guide/basic.html, with English text "hello world", and it worked fine. Without your code, there is no way to answer that question. By the way - nor #encoding: -*- utf-8 -*- is a magic to solve everything that has accents, nor has any effect on Python 3.

1 answer

1


I did some tests here, including using a file . Kv, and everything works perfectly. but I’m on a Unix-like system and you’re probably on Windows.

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

Otherwise, what happens is Kivy is trying to open the file in the native system encoding, indicated by Python - and in the case of Windows, it is not "utf-8". A quick "workaround" would be to record your file. Kv in native encoding (latin1), but this would imply that operating systems using modern character sets by default such as macOS, Linux, Android and even a future version of Windows would have problems with their app.

Good - I did some searches, and you can see that this is probably the case: https://github.com/kivy/kivy/issues/5154

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

For this, instead of having the file name . Kv equal to the name of its class "App", as it is necessary for kivy to load the file automatically, put any other name in the file . Kv, and in the method .build from 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 file . Kv is now under your control, so just use the open Python, forcing text encoding to utf-8.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.