我正在尝试创建一个基本的Kivy应用,它在GridLayout中有4个标签。这是我的Python代码:
from kivy.lang import Builder
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
class MyLayout(GridLayout):
def __init__(self, **kwargs):
Builder.load_file("mylayout.kv")
super(MyLayout, self).__init__(**kwargs)
class MyApp(App):
def build(self):
return MyLayout()
if __name__ == '__main__':
MyApp().run()
这是我的mylayout.kv文件:
<MyLayout>:
GridLayout:
cols: 2
rows: 4
Label:
text: "Hello"
Label:
text: "Hello2"
Label
text: "Hello3"
Label
text: "Hello4"
当我运行程序时,会出现在日志中:
[INFO ] [Logger ] Record log in C:\Users\tjordan\.kivy\logs\kivy_15-09-22_56.txt
[INFO ] [Kivy ] v1.9.0
[INFO ] [Python ] v2.7.10 (default, May 23 2015, 09:40:32) [MSC v.1500 32 bit (Intel)]
[INFO ] [Factory ] 173 symbols loaded
[INFO ] [Image ] Providers: img_tex, img_dds, img_gif, img_sdl2 (img_pil, img_ffpyplayer ignored)
[INFO ] [Text ] Provider: sdl2
[INFO ] [OSC ] using <thread> for socket
[INFO ] [Window ] Provider: sdl2
[INFO ] [GL ] GLEW initialization succeeded
[INFO ] [GL ] OpenGL version <3.1.0 - Build 9.17.10.4229>
[INFO ] [GL ] OpenGL vendor <Intel>
[INFO ] [GL ] OpenGL renderer <Intel(R) HD Graphics>
[INFO ] [GL ] OpenGL parsed version: 3, 1
[INFO ] [GL ] Shading version <1.40 - Intel Build 9.17.10.4229>
[INFO ] [GL ] Texture max size <8192>
[INFO ] [GL ] Texture max units <16>
[INFO ] [Shader ] fragment shader: <No errors.>
[INFO ] [Shader ] vertex shader: <No errors.>
[INFO ] [Shader ] program: <No errors.>
[INFO ] [Window ] auto add sdl2 input provider
libpng warning: iCCP: known incorrect sRGB profile
[INFO ] [Window ] virtual keyboard not allowed, single mode, not docked
[INFO ] [Base ] Start application main loop
[INFO ] [GL ] NPOT texture support is available
[WARNING ] <__main__.MyLayout object at 0x02D0F260> have no cols or rows set, layout is not triggered.
[INFO ] [Base ] Leaving application in progress...
我是Kivy的新手,所以我只是试图了解基础知识。我已经尝试将Builder从 init 移出,但仍然得到相同的“没有设置cols或行,没有触发布局”错误。当程序运行时,它会将所有标签放在左下角的彼此顶部。
答案 0 :(得分:3)
您的kv规则将GridLayout放置在GridLayout中,并且仅为第二个设置行和列。只需删除第二级,以便为第一个GridLayout(您的MyLayout)设置cols,并将标签直接添加到此。