2016-09-22 9 views
1

私はkivyを使って画面に4つの画像を表示するプログラムを書いています。私は取得したい ターゲットウィンドウは次のようである:ウィンドウの背景が白であり、そして4枚の画像は緑色rectangles.The画面比率で示されている targetkivy GridLayoutとAnchorLayout

は16:9が、画像の比率が4であります:3。私はこれらの4つの画像を画面の中央に貼りたいです。

私は、コードが以下の通りである、のGridLayoutとAnchorLayoutを使用してみました:

from kivy.app import App 
from kivy.uix.image import Image 
from kivy.config import Config 
from kivy.uix.anchorlayout import AnchorLayout 
from kivy.uix.gridlayout import GridLayout 

class MainScreen(GridLayout): 

    def __init__(self, **kwargs): 

     super(MainScreen, self).__init__(**kwargs) 

     self.float_list = [] 
     self.anchor_list = [] 
     self.image_list = [] 
     self.rows = 2 

     for i in range(4): 
      if i % 2 == 0: 
       self.anchor_list.append(AnchorLayout(anchor_x='right')) 
      else: 
       self.anchor_list.append(AnchorLayout(anchor_x='left')) 

      self.image_list.append(Image(source='./tmp_pics/1.jpg')) 

      self.anchor_list[i].add_widget(self.image_list[i]) 
      self.add_widget(self.anchor_list[i]) 

class MainDisplay(App): 

    def __init__(self): 
     super(MainDisplay, self).__init__() 

    def build(self): 

     self.mainScreen = MainScreen() 

     return self.mainScreen 

if __name__ == '__main__': 

    Config.set('graphics', 'width', '1600') 
    Config.set('graphics', 'height', '900') 

    app = MainDisplay() 
    app.run() 

しかし、私が得た結果は次のとおりです。 result

誰もが上記の要件を取得するために私を助けることができますしてください?

答えて

0

キーは、画面の中央にセンタリングされる単一のグリッドレイアウトにこれらの画像のすべてを置くことである。

#!/usr/bin/env python3.5 
# -*- coding: utf-8 -*- 
from kivy.app import App 
from kivy.lang import Builder 

gui = ''' 
Screen: 

    GridLayout: 
     cols: 2 
     rows: 2 
     size_hint: None, None 
     size: self.minimum_size 
     pos_hint: {'center_x': 0.5, 'center_y': 0.5} 

     MyImage 
     MyImage 
     MyImage 
     MyImage 

<[email protected]> 
    size_hint: None, None 
    size: 400, 200 
    source: 'http://lorempixel.com/400/200/cats/' 
''' 


class Test(App): 

    def build(self): 
     return Builder.load_string(gui) 


Test().run() 
関連する問題