2017-08-24 54 views
0

我有一些小部件内floatlayout:强制FloatLayout为正方形?

fl = FloatLayout(size=(600,600), 
        pos_hint=({'center_x': .5, 'center_y': .5})) 

    pos1 = ImgButton(source='zulgrey.png', 
        size_hint=(0.2,0.2), 
        pos_hint=({'center_x': 0, 'center_y': .43})) 

    pos2 = ImgButton(source='zulgrey.png', 
        size_hint=(0.2,0.2), 
        pos_hint=({'center_x': 0.5, 'center_y': 0.93})) 

    pos3 = ImgButton(source='zulgrey.png', 
        size_hint=(0.2,0.2), 
        pos_hint=({'center_x': 1, 'center_y': .43})) 

    pos4 = ImgButton(source='zulgrey.png', 
        size_hint=(0.2,0.2), 
        pos_hint=({'center_x': 0.5, 'center_y': .43})) 
    fl.add_widget(cove) 
    fl.add_widget(pos1) 
    fl.add_widget(pos2) 
    fl.add_widget(pos3) 
    fl.add_widget(pos4) 

然而,当我调整窗口小部件的位置,从那里我预期他们能够移动。有没有一种方法可以强制floatlay在调整大小时具有固定的纵横比?

回答

0

解决这样的:

FloatLayout: 
    size_hint: 1, None 
    size: self.width, self.width 

size_hint:1不必是一个,我只是想我的广场填补了FloatLayout的大小。

或者:

FloatLayout: 
    size_hint: None, 1 
    size: self.height, self.height 
0

嗯,是的,有间接和一些基本的数学。

将小部件放到另一个FloatLayout中,并将此floatlayout的大小绑定到函数以更新当前的floatlayout。

def update_size(self, inst, size): 
    target_ratio = 16/9. 
    width, height = size 
    # check which size is the limiting factor 
    if width/height > target_ratio: 
     # window is "wider" than targeted, so the limitation is the height. 
     fl.height = height 
     fl.width = height * target_ratio 
    else: 
     fl.width = width 
     fl.height = width/target_ratio 

要在fl禁用size_hint这样可以有效地设置其大小的方法,另外,你一定要FL其父布局为中心。

fl.size_hint = None, none 
fl.pos_hint = {'center': (.5, .5)}