2017-07-14 122 views
1

我想對齊在我的測試用戶界面標籤和按鈕這是我的KV文件如何在kivy中使用pos_hint和FloatLayout?

<test>: 

    Label:   
     text: "foo" 
     color: 0,1,0,1 
     #pos:120,20 
     pos_hint:{"right":0.1,"top":1} 
    Label: 
     text:"boo" 
     color: 0,0,1,1 
     #pos:80,20 
     pos_hint:{"right":0.1,"top":0.5} 

    Label: 
     text:"bar" 
     color: 1,0,0,1 
     #pos:20,120 
     pos_hint:{"right":0.1,"top":0.1} 
    Button: 
     text:"goo" 
     size_hint:0.1,0.1 

我能夠成功地創建標籤FOO,噓聲和酒吧使用POS但是當我用pos_hint它返回空白輸出?

回答

3

由於標籤的文字在屏幕外(標籤本身是透明的),您正在獲得「空白」輸出。

  1. 由於您的佈局<test>沒有size_hint因此需要對 默認的(1,1)這使得它的Window的大小(這是 800 x 600)。
  2. 您的標籤也沒有size_hint,因此它們默認爲其父級的size - 佈局,因此它們最終具有size [800, 600]。標籤中的文本默認居中,其背景是透明的。 (也許你應該先用按鈕試試這個讓你有尺寸的可視化表示)
  3. 因此,文本pos = (0,0)標籤將出現在屏幕中央

然後我們有pos_hint採取不同的參數(以下描述可能不適合的東西是準確的FloatLayout外):

pos_hint:{"right":v1,"top":v2}設置pos(self.parent.right*v1 - self.width, self.parent.top*v2 - self.height) - 您設置日的topright你正在放置的小部件。因此,你的標籤得到這樣的負座標,他們的文本不會出現在屏幕上(因爲左下爲0,0

那麼我們有pos_hint:{"x":v1,"y":v2}(你可能會發現你的情況比較有用),和pos_hint:{"center_x":v1,"center_y":v2}。你應該能夠找出它們是如何工作銘記的大小會影響事情的樣子,因爲只有pos設置左下方座標..你可以玩這個.kv文件:

#:kivy 1.0.9 

<test>: 
    #size: (500, 500) 
    #size_hint:(None, None) 
    canvas: 
     Color: 
      rgb: 1,0,0 
     Rectangle: 
      size: (5,5) 
      pos: (0,0) 

    Widget: 
     id:wig 
     pos: (250,250) 
     canvas: 
      Color: 
       rgb: 1,1,1 
      Rectangle: 
       size: (5,5) 
       pos: self.pos 

    Label: 
     id: boo 
     text:"boo" 
     color: 0,0,1,1 
     #size_hint:(1,1) 
     pos_hint:{"center_x":1,"center_y":1} 

    Label: 
     id: foo 
     text: "foo" 
     color: 0,1,0,1 
     #size_hint: (.6,.6) 
     pos_hint:{"x":1,"y":1} 

    Label: 
     id: bar 
     text:"bar" 
     color: 1,0,0,1 
     #size:(500,500) 
     #size_hint:(None, None) 
     pos_hint:{"right":1,"top":1} 
     #pos:100, 10 


    Button: 
     text:"goo" 
     size_hint:0.1,0.1 
     pos:(1,1) 
     #some debug info, i know the code is ugly 
     on_press: print self.parent.size,'\n', self.parent.right, self.parent.top, self.parent.x, self.parent.y, self.parent.center_x, self.parent.center_y, "\n","bar_right_top:", bar.pos,"foo_x_y:", foo.pos,"boo_center:", boo.pos, "\nwhite square:", wig.pos, "\n", bar.size, foo.size, boo.size