A
A
Anastasia Taube2020-03-10 09:19:06
Python
Anastasia Taube, 2020-03-10 09:19:06

How to solve UnicodeDecodeError for cyrillic in kv file?

Given:
This tutorial. In English it works without nit-picking, in Russian - no. :\ Don't be silly, I googled and found the solution .
And after all, in theory, not bad: manually read the file in the desired encoding. But it was not there. It works for everyone, but not for me (as always). :D
Warning the proposal on the latest proposed solution in the branch - prescribe the desired text in the .ru-file, and then refer to it in .kv. It is not even considered, because this example was needed to understand who this kv of yours is. Understood. And then the entire interface should be in Russian (I would write in English, but the commission on the defense of the diploma will hardly understand this attack. :D).
The code:

spoiler
#main.py
#  -*- coding: utf-8 -*-
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.lang import Builder
from kivy.uix.widget import Widget

class MyGrid(Widget):
    pass

with open("my.kv", encoding='utf8') as f:
    my = Builder.load_string(f.read())

class MyApp(App):
    def build(self):
        return my

if __name__ == "__main__":
    MyApp().run()

#my.kv

#encoding: utf-8
<MyGrid>:
    GridLayout:
        cols:1
        size: root.width, root.height

        GridLayout:
            cols:2

            Label:
                text: "Имя: "

            TextInput:
                multinline:False

            Label:
                text: "Е-майл: "

            TextInput:
                multiline:False

        Button:
            text:"Submit"

Actually, how exactly it swears:
spoiler
5e672e0d93588167365610.png


Question: How to make it read everything correctly and finally output Cyrillic, because it displays English without problems? :(

(If this is important (and this is almost always important, as experience shows), I have a Mac, and I'm still getting used to it and absolutely do not understand what and how to change it, so I would be very grateful if you give a link / instruction , should it become necessary.)
spoiler
(Можете пошутить, мол, поменяй на нормальный комп и, желательно, на Linux, но я играю на опережение и шучу за вас :D)


UPD. Be sure to read the comments to the solution, otherwise it does not work.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
javedimka, 2020-03-10
@Tayaki

Judging by the error and the code, your python swears not at your decision:

with open("my.kv", encoding='utf8') as f:
    my = Builder.load_string(f.read())

And on the internal guts of kivy. If you go to the github issues, the link to which is attached, and find this solution there, the author also writes that you need to turn off autoloading the file, otherwise the error will still be thrown, it’s more correct to rename the file so that the load_kv method cannot find it:

For example, say you have a file named main.py that contains::
class ShowcaseApp(App):
    pass

This method will search for a file named `showcase.kv` in
the directory that contains main.py. The name of the kv file has to be
the lowercase name of the class, without the 'App' postfix at the end
if it exists.

Accordingly, you need to rename the file from my.kv to some ym.kv and use the proposed solution.
Or you can override the load_kv method:
class MyApp(App):
    def build(self):
        return my
    def load_kv(self, *args, **kwargs):
        pass

But! This is wrong , because who knows where else it is used, even though the documentation says that it is called once at the start of the application - I would not trust it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question