Kivy-kv语言VKeyboard

Posted

技术标签:

【中文标题】Kivy-kv语言VKeyboard【英文标题】:Kivy- kv language VKeyboard 【发布时间】:2017-01-16 22:07:03 【问题描述】:

我想为我使用 Kivy 1.9.0 编写的应用添加 VKeyboard 小部件。我使用 Python 2.7.12。有没有办法通过 kv 语言将此小部件添加到应用程序?因为在尝试下面的方法时存在错误:'ValueError: No JSON object could be decoded'

        Button:
            background_color:1,0,0,0.5
            text:'Next word'
            size_hint:.5,.2
            font_size:25
            pos_hint:'center_x':.5
            on_press:root.word_dict()

        VKeyboard:
            layout:'layout.json'

layout.json

  "title":"KeyboardPinyin", "description":"Keyboard using for writing pinyin characters", "cols":5, "rows":3, "normal_1":[ ["ā","ā","ā",1], ["ē","ē","ē",1], ["ī","ī","ī",1], ["ō","ō","ō",1], ["ū","ū","ū",1] ], "normal_2": [ ["á","á","á",1], ["é","é","é",1], ["í","í","í",1], ["ó","ó","ó",1], ["ú","ú","ú",1] ], "normal_3": [ ["ǎ","ǎ","ǎ",1], ["ě","ě","ě",1], ["ǐ","ǐ","ǐ",1], ["ǒ","ǒ","ǒ",1], ["ǔ","ǔ","ǔ",1] ], "normal_4": [ ["à","à","à",1], ["è","è","è",1], ["ì","ì","ì",1], ["ò","ò","ò",1], ["ù","ù","ù",1] ] 

【问题讨论】:

听起来 layout.json 不是有效的 json ...确保您遵循他们的示例 .... 或至少发布 layout.json 有 layout.json: "title":"KeyboardPinyin", "description":"用于书写拼音字符的键盘", "cols":5, "rows":3, "normal_1" :[ ["ā","ā","ā",1], ["ē","ē","ē",1], ["ī","ī","ī",1], ["ō","ō","ō",1], ["ū","ū","ū",1] ], "normal_2": [ ["á","á","á" ,1], ["é","é","é",1], ["í","í","í",1], ["ó","ó","ó",1 ], ["ú","ú","ú",1] ], "normal_3": [ ["ǎ","ǎ","ǎ",1], ["ě","ě"," ě",1], ["ǐ","ǐ","ǐ",1], ["ǒ","ǒ","ǒ",1], ["ǔ","ǔ","ǔ" ,1] ], "normal_4": [ ["à","à","à",1], ["è","è","è",1], ["ì","ì" ,"ì",1], ["ò","ò","ò",1], ["ù","ù","ù",1] 【参考方案1】:
Traceback (most recent call last):
  File "C:/Users/joran/.PyCharm50/config/scratches/scratch_33", line 3, in <module>
    data = json.load(open(os.path.expanduser("~/layout1.json")))
  File "C:\Python27\lib\json\__init__.py", line 290, in load
    **kw)
  File "C:\Python27\lib\json\__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "C:\Python27\lib\json\decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Python27\lib\json\decoder.py", line 382, in raw_decode
    obj, end = self.scan_once(s, idx)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xe1 in position 0: unexpected end of data

您不能尝试使用 utf8 解码输入...您的 json 中有非 utf8 字符

您应该改用 unicode 表示 "\u00e0" 等(或者您必须以某种方式指定 json 的字符编码......我不确定您如何临时做到这一点)

【讨论】:

在unicode中写入字符后仍然出现问题。 什么意思?请为其他人发布您的解决方案,如果正确,请接受此解决方案 我刚刚用按钮制作了 GridLayout(而不是 VKeyboard),每个按钮都包含我需要的字符,并在按下时将该字符放入 TextInput。我知道这不是问题的“真正”解决方案,但它至少有效。

以上是关于Kivy-kv语言VKeyboard的主要内容,如果未能解决你的问题,请参考以下文章

python——静态语言动态语言强类型语言弱类型语言脚本语言解释型语言编译型语言

动态语言和静态语言编译型语言和解释型语言强类型语言和弱类型语言的分析

C语言和其他高级语言有啥异同?

机器语言汇编语言高级语言

动态语言,静态语言,强类型定义语言,弱类型定义语言

简述啥是源语言,目标语言,中间语言