2017-01-16 31 views
0
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文件如下:無法做一個簡單的網格佈局與kivy 4個按鈕

<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" 

,但我得到的錯誤如下: 文件「/ usr/lib目錄/ python3/dist-packages/kivy/lang.py「,第1440行,parse_level '聲明後無效數據') kivy.lang.ParserException:解析器:文件」/home/dimple/ucoe.kv「,第4行: ... 2:GridLayout: 3:cols:2

4:row_force_default:真 5:row_default_height:40 6:
...聲明 後 無效數據請幫幫忙,我是kivy一個新手。

回答

0

年代碼有很多bug。

  • 導入按鈕
  • 下按鈕寬度.kv = 100(錯誤的)
  • 除去GridLayout的在.kv它不是必需的。

以下是更新後的運行代碼。

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文件=>

<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" 
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

<[email protected]yout>: 
    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