2017-07-14 125 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