无法在kivy中为动态创建的小部件赋予新属性

时间:2014-11-04 01:27:07

标签: android python kivy

我使用kivy编写程序,我认为它应该可行,但它没有。

代码:

main.py:

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.carousel import Carousel
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.pagelayout import PageLayout
from kivy.uix.scrollview import ScrollView

class rootlayout(BoxLayout):
    pass
class topbar(BoxLayout):
    pass
class body(Carousel):
    pass

class myApp(App):
    def build(self):
        return rootlayout()


if __name__ == '__main__':
    myApp().run()

my.kv:

<rootlayout>:
    height:root.height
    spacing:0
    orientation:"vertical"
    Button:
        size_hint:1,0
    topbar:
    body:

<body>:
    size_hint:1, .9
    onepage:
        id:pp
    ScrollView:
        id:scrollid2
        do_scroll_x:False
        GridLayout:
            height: self.minimum_height
            cols:1
            size_hint_y:None
            Button:
                height:root.height * 2
                size_hint_y:None
    ScrollView:
        id:scrollid3
        do_scroll_x:False
        GridLayout:
            height: self.minimum_height
            cols:1
            size_hint_y:None
            Button:
                height:scrollid3.height * 2
                size_hint_y:None

<topbar>:
    size_hint: 1, .1

    orientation:"horizontal"
    Button:
        size_hint:.9,1
        text:"root"
    test:
        size_hint:.1,1
        text:"root"

<onepage@ScrollView>:
    do_scroll_x:False
    GridLayout:
        height: self.minimum_height
        cols:1
        size_hint_y:None
        Button:
            height:scrollid1.height * 2
            size_hint_y:None
<test@Button>:

错误讯息:

  paul@paul-ThinkPad-Edge-E530:~/myProgram/homework/manufacturingExperience/android/ask/test4$ sudo python main.py
[INFO   ] Kivy v1.8.0
[INFO   ] [Logger      ] Record log in /home/paul/.kivy/logs/kivy_14-11-04_39.txt
[INFO   ] [Factory     ] 157 symbols loaded
[DEBUG  ] [Cache       ] register <kv.lang> with limit=None, timeout=Nones
[DEBUG  ] [Cache       ] register <kv.image> with limit=None, timeout=60s
[DEBUG  ] [Cache       ] register <kv.atlas> with limit=None, timeout=Nones
[INFO   ] [Image       ] Providers: img_tex, img_dds, img_pygame, img_pil, img_gif 
[DEBUG  ] [Cache       ] register <kv.texture> with limit=1000, timeout=60s
[DEBUG  ] [Cache       ] register <kv.shader> with limit=1000, timeout=3600s
[INFO   ] [Text        ] Provider: pygame
[DEBUG  ] [Window      ] Ignored <egl_rpi> (import error)
[INFO   ] [Window      ] Provider: pygame(['window_egl_rpi'] ignored)
[DEBUG  ] [Window      ] Display driver x11
[DEBUG  ] [Window      ] Actual window size: 800x600
[DEBUG  ] [Window      ] Actual color bits r8 g8 b8 a8
[DEBUG  ] [Window      ] Actual depth bits: 24
[DEBUG  ] [Window      ] Actual stencil bits: 8
[DEBUG  ] [Window      ] Actual multisampling samples: 4
[INFO   ] [GL          ] OpenGL version <3.0 Mesa 10.1.3>
[INFO   ] [GL          ] OpenGL vendor <Intel Open Source Technology Center>
[INFO   ] [GL          ] OpenGL renderer <Mesa DRI Intel(R) Ivybridge Mobile >
[INFO   ] [GL          ] OpenGL parsed version: 3, 0
[INFO   ] [GL          ] Shading version <1.30>
[INFO   ] [GL          ] Texture max size <8192>
[INFO   ] [GL          ] Texture max units <16>
[DEBUG  ] [Shader      ] Fragment compiled successfully
[DEBUG  ] [Shader      ] Vertex compiled successfully
[DEBUG  ] [ImagePygame ] Load </usr/lib/python2.7/dist-packages/kivy/data/glsl/default.png>
[INFO   ] [Window      ] virtual keyboard not allowed, single mode, not docked
[DEBUG  ] [App         ] Loading kv <./my.kv>
 Traceback (most recent call last):
   File "main.py", line 22, in <module>
     myApp().run()
   File "/usr/lib/python2.7/dist-packages/kivy/app.py", line 765, in run
     self.load_kv(filename=self.kv_file)
   File "/usr/lib/python2.7/dist-packages/kivy/app.py", line 585, in load_kv
     root = Builder.load_file(rfilename)
   File "/usr/lib/python2.7/dist-packages/kivy/lang.py", line 1444, in load_file
     return self.load_string(data, **kwargs)
   File "/usr/lib/python2.7/dist-packages/kivy/lang.py", line 1491, in load_string
     parser = Parser(content=string, filename=fn)
   File "/usr/lib/python2.7/dist-packages/kivy/lang.py", line 1049, in __init__
     self.parse(content)
   File "/usr/lib/python2.7/dist-packages/kivy/lang.py", line 1126, in parse
     rule.precompile()
   File "/usr/lib/python2.7/dist-packages/kivy/lang.py", line 913, in precompile
     x.precompile()
   File "/usr/lib/python2.7/dist-packages/kivy/lang.py", line 843, in precompile
     self.co_value = compile(value, self.ctx.filename or '<string>', mode)
   File "./my.kv", line 14
     id:pp
       ^
 SyntaxError: invalid syntax

在代码中,我看到测试小部件有效,但是一个页面没有。我使用ubuntu 14.04,kivy 1.8,并从ppa:kivy-team / kivy安装了kivy。 谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

您的窗口小部件类名称需要以大写字母开头。无论如何,在Python中使用CamelCase命名您的类是标准做法(即RootLayout而不是rootlayout),但是kv解析器没有其他方法可以告诉属性除了小部件。因此,如果声明的第一个字符是小写的,则解析器假定声明属于属性。所以这个:

<body>:
    onepage:
        id:pp

被解释为尝试将onepage小部件的body属性设置为值id:pp,这不是有效的Python表达式。如果您将onepage重命名为OnePage,则可以这样做:

<body>:
    OnePage:
        id:pp