from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
class Grid(GridLayout):
pass
class UcoeApp(App):
def build(self):
return Grid()
UcoeApp().run()
ucoe.kv文件如下:
<Grid>:
GridLayout:
cols:2
row_force_default:True
row_default_height:40
Button:
text:"hello1"
size_hint_x:None
width=100
Button:
text:"world1"
Button:
text:"hello2"
size_hint_x:None
width:100
Button:
text:"world2"
但我得到的错误如下: 在parse_level中的文件“/usr/lib/python3/dist-packages/kivy/lang.py”,第1440行 '声明后的数据无效') kivy.lang.ParserException:Parser:文件“/home/dimple/ucoe.kv”,第4行: ... 2:GridLayout: 3:cols:2
4:row_force_default:True 5:row_default_height:40 6:
... 声明后的数据无效 请帮忙,我是kivy的新手。
答案 0 :(得分:0)
你的代码中有很多错误。
以下是更新并运行的代码。
from kivy.uix.button import Button
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
class Grid(GridLayout):
pass
class UcoeApp(App):
def build(self):
return Grid()
UcoeApp().run()
ucoe.kv file =&gt;
<Grid>:
cols:2
row_force_default:True
row_default_height:40
Button:
text:"hello1"
size_hint_x:None
width:100
Button:
text:"world1"
Button:
text:"hello2"
size_hint_x:None
width:100
Button:
text:"world2"
答案 1 :(得分:0)
根据需要进行编辑。
main.py
from kivy.uix.button import Button
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.lang import Builder
class Grid(GridLayout):
pass
presentation = Builder.load_file("main.kv")
class UcoeApp(App):
def build(self):
return Grid()
UcoeApp().run()
main.kv
<Grid@GridLayout>:
cols:2
Button:
text:"hello1"
size: self.size
Button:
text:"world1"
size: self.size
Button:
text:"hello2"
size: self.size
Button:
text:"world2"
size: self.size