安卓上的Kivy:parser.py create_missing异常:类型错误:参数'name'的类型不正确(预期的str,得到unicode)



我的Kivy应用程序立即在Android上崩溃。我想这是一些 Python2/Python3 不兼容,但我不知道此错误的实际原因是什么(adb logcat 输出(:

01-28 14:00:39.421  7457  7492 I python  :    File "/home/kivy/Pfadfinder/.buildozer/android/app/main.py", line 5, in <module>
01-28 14:00:39.421  7457  7492 I python  :    File "/home/kivy/Pfadfinder/.buildozer/android/platform/build/dists/pfadfinder/private/lib/python2.7/site-packages/kivy/app.py", line 802, in run**strong text**
01-28 14:00:39.421  7457  7492 I python  :    File "/home/kivy/Pfadfinder/.buildozer/android/app/pfadfinder/hauptmenue.py", line 152, in build
01-28 14:00:39.422  7457  7492 I python  :    File "/home/kivy/Pfadfinder/.buildozer/android/platform/build/dists/pfadfinder/private/lib/python2.7/site-packages/kivy/lang/builder.py", line 382, in load_string
01-28 14:00:39.422  7457  7492 I python  :    File  "/home/kivy/Pfadfinder/.buildozer/android/platform/build/dists/pfadfinder/private/lib/python2.7/site-packages/kivy/lang/builder.py", line 496, in _apply_rule
01-28 14:00:39.422  7457  7492 I python  :    File "/home/kivy/Pfadfinder/.buildozer/android/platform/build/dists/pfadfinder/private/lib/python2.7/site-packages/kivy/lang/parser.py", line 281, in create_missing
01-28 14:00:39.423  7457  7492 I python  :  TypeError: Argument 'name' has incorrect type (expected str, got unicode)

该应用程序是在Windows上使用Python 3.6开发的。

在Android上,它运行在Python 2.7.2(Kivy v1.10.0(中。

注意:我是德国人,所以该应用程序应该可以使用变音符号 ä ö ü。我正在像这样加载KV文件:

def build(self):
    with io.open(os.path.join(THIS_DIR, "hauptmenue.kv"), encoding='utf-8') as f:
       screen_management = Builder.load_string(f.read())

最后一行是堆栈跟踪中提到的第 152 行。

KV 文件如下所示:

:基维 1.1.0

ScreenManagement:
    hauptmenue: hauptmenue
    MainMenu:
        id: hauptmenue
<LevelButton>:
    font_size: "40dp"

<MainMenu>:
    name: "hauptmenue"
    level_auswahl: level_auswahl
    BoxLayout:
        orientation: "vertical"
        size: root.size
        Label:
            text: "Pfadfinder"
            size_hint: (1.0, None)
            font_size: "30dp"
            height: "40dp"
        GridLayout:
            id: level_auswahl
            rows: 6
            cols: 5
        BoxLayout:
            orientation: "horizontal"
            size_hint: (1.0, None)
            height: "40dp"
            Button:
                text: "Einstellungen"
                on_release: app.open_settings()
                font_size: "30dp"
            Button:
                text: "Beenden"
                on_release: app.stop()
                font_size: "30dp"
<Einstellungen>:
    name: "einstellungen"
    Settings:
        on_close: app.close_settings()

我会回答我自己的问题。

错误消息确实不是很有帮助,所以我试图记住自上一个工作版本以来发生了哪些变化。

加载 KV 文件的方法没有改变 - 就像前几天一样 - 原因是 KV 文本中的变音符号在 Windows 上无法正确呈现。

我添加了屏幕管理,应用程序配置和JsonStore,例如播放器的昵称。当然,我用了变音符号作为这个名字,所以我认为这些是罪魁祸首。但这是错误的线索。

原因实际上是我在 KV 文件中的屏幕有一个"名称"属性。

实际上,有帮助的是修改以 Python 版本依赖的方式加载 KV 文件的方法:

fname = os.path.join(THIS_DIR, "hauptmenue.kv")
if sys.version_info[0] >= 3:
    with io.open(fname, encoding='utf-8') as f:
        screen_management = Builder.load_string(f.read())
else:
    screen_management = Builder.load_file(fname)

希望这对其他人有所帮助...

最新更新