我正在尝试在界面中使用西里尔文,但应用程序无法启动。这是代码:
主文件
# -*- coding: utf-8 -*-
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
import sys
sys.setdefaultencoding('utf-8')
class MainScreen(Screen):
pass
class AnotherScreen(Screen):
pass
class ScreenManagement(ScreenManager):
pass
presentation = Builder.load_file("main.kv")
class MainApp(App):
def build(self):
return presentation
MainApp().run()
主文件
# -*- coding: utf-8 -*-
ScreenManagement:
MainScreen:
AnotherScreen:
<MainScreen>:
name: 'main'
Button:
on_release: app.root.current = 'other'
text: 'Другой экран'
font_size: 50
<AnotherScreen>:
name: 'other'
Button:
on_release: app.root.current = 'main'
text: 'back to the home screen'
font_size: 50
当我运行android程序时,我得到:
[INFO ] [Kivy ] v1.9.2-dev0
[INFO ] [Python ] v3.6.2 (default, Oct 29 2017, 05:27:57)
[GCC 7.2.0]
[INFO ] [Factory ] 193 symbols loaded
[INFO ] [Image ] Providers: img_tex, img_dds, img_gif, img_sdl2 (img_pil, img_ffpyplayer ignored)
Traceback (most recent call last):
File "/storage/emulated/0/Download/TrafficTaxApp/TrafficTaxApp/main.py", line 19, in
presentation = Builder.load_file("main.kv")
File "/data/data/ru.iiec.pydroid3/files/i686-linux-android/lib/python3.6/site-packages/kivy/lang/builder.py", line 290, in load_file
data = fd.read()
File "/data/data/ru.iiec.pydroid3/files/i686-linux-android/lib/python3.6/encodings/ascii.py", line 26, in decode
return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in position 186: ordinal not in range(128)
在 Ubuntu 上一切都很好,没有问题。
有什么问题?
并且布局不需要指定编码。顺便说一下,你是如何在 PyDroid3 中安装 pygame 的?
布局编码错误在 Windows 上很常见。Windows 上的 Python 3 默认以 win-1251 编码打开文件。为了让它在 utf-8 中打开,你必须强制 open(filename, encoding='utf-8') Kivy 打开而不指定编码,即 您自己需要打开一个带有编码的 kv 文件,将其读入一个变量并将其传递给 Builder。
它是这样工作的:
主文件
演示文稿.kv