2017-12-31 60 views
1

これは私の最初の投稿ですが、私は可能な限り詳細にしようとします。 私のアプリケーションはPythonで、kivyのグリッドが含まれています。グリッド内の各要素には、4つのウィジェットと5つ目の可能性があります。 4つの追加のウィジェットは、エッジでは十字形、中間では五番目のウィジェットとされています。私はこれまでのところ、メインウィンドウ複数のウィジェットを含む各グリッド要素を持つKivyの動的グリッド

の位置0,0にとても良いことが左下隅に土地のサブウィジェットを追加するたびに

問題は、あります。ちょうど私はちょうど別のウィジェット内のウィジェットを正しく表示することを試みています。

私が試みたものをHERESに

<[email protected]> 
    BoxLayout: 
     orientation:'horizontal' 
     Button: 
      text:'One' 
      size:10,10 
      size_hint:None,None 

その後、.kvの私はそれの内側のボックスレイアウトを置く私のアプリのためのファイル、およびボタンを構築します。

はまた、私は次のクラスの設定を試してみました:

class GridCell(Label): 

def __init__(self, **kwargs): 
    super().__init__(**kwargs) 
    self.root = FloatLayout() 
    self.button = Button(text="test") 
    self.button.x = self.root.x 
    self.button.center_y = self.root.center_y 
    self.root.add_widget(self.button) 
    self.add_widget(self.root) 

も動作しませんでした。

私は、forループの各反復のために新たに作成されたウィジェットを持つグリッド上.add呼び出すことによって、グリッドセルを追加しています。

すべての子ウィジェットは明らかに作成されていますが、それらはすべて左下隅にあります!

import kivy 
import World 
from kivy.app import App 
from kivy.uix.label import Label 
from kivy.uix.button import Button 
from kivy.uix.gridlayout import GridLayout 
from kivy.uix.floatlayout import FloatLayout 
from kivy.graphics import Color, Rectangle 

kivy.require('1.10.0') 



class GridCell(Label): 

def __init__(self, **kwargs): 
    super().__init__(**kwargs) 
    self.root = FloatLayout() 
    self.button = Button(text="blargh") 
    self.button.x = self.root.x 
    self.button.center_y = self.root.center_y 
    self.root.add_widget(self.button) 
    self.add_widget(self.root) 

def on_size(self, *args): 
    self.canvas.before.clear() 

    if self.id is "cliff": 
     with self.canvas.before: 
      Color(249/255, 6/255, 6/255, 0.3) 
      Rectangle(pos=self.pos, size=self.size) 
    if self.id is "goal": 
     with self.canvas.before: 
      Color(6/255, 6/255, 249/255, 0.3) 
      Rectangle(pos=self.pos, size=self.size) 
    if self.id is "start": 
     with self.canvas.before: 
      Color(11/255, 174/255, 6/255, 0.3) 
      Rectangle(pos=self.pos, size=self.size) 
    if self.id is "normal": 
     with self.canvas.before: 
      Color(119/255, 115/255, 115/255, 0.3) 
      Rectangle(pos=self.pos, size=self.size) 




class GameGridApp(App): 

def __init__(self, **kwargs): 
    super().__init__(**kwargs) 
    self.grid = GridLayout(cols=8, rows=5) 
    self.load_into() 

def load_into(self): 
    world = World.World(8, 5) 
    world.build_gamegrid() 

    for cell in world.gamegrid: 
     name = str(cell.name) 
     grid_cell = GridCell() 
     grid_cell.text = name 

     if cell.start: 
      grid_cell.id = "start" 
     if cell.goal: 
      grid_cell.id = "goal" 
     if cell.cliff: 
      grid_cell.id = "cliff" 
     if cell.field: 
      grid_cell.id = "normal" 

     self.grid.add_widget(grid_cell) 

def build(self): 
    return self.grid 


customLabel = GameGridApp() 
customLabel.run() 
+0

こんにちは@trashtatur。ですから、各グリッドに 'GridLayout'オブジェクトを持つ方法を知りたいのですが、その中に4/5ウィジェットがありますか?例えば、4分の5の 'Label'オブジェクトなどです。あなたの質問を明確にしたり、目立つようにしたいかもしれません。 – Arief

答えて

0

私はアイデアを与える可能性があり、「サブグリッド」を含む「サブグリッド」オブジェクトと「メイングリッド」オブジェクトを作成します。

これは今、GUIの全体のコードがあります。これらの2つのオブジェクトはGridLayoutのオブジェクトになります。ここで

python2.7で簡単な例です:

import kivy 
from kivy.app import App 
from kivy.uix.gridlayout import GridLayout 
from kivy.uix.label import Label 

class SubGrids(GridLayout): 
    def __init__(self): 
     GridLayout.__init__(self, cols=3, rows=3); 
     self.add_widget(Label(text='1st')); 
     self.add_widget(Label(text='')); 
     self.add_widget(Label(text='2nd')); 
     self.add_widget(Label(text='')); 
     self.add_widget(Label(text='3rd')); 
     self.add_widget(Label(text='')); 
     self.add_widget(Label(text='4th')); 
     self.add_widget(Label(text='')); 
     self.add_widget(Label(text='5th')); 
class Grids(GridLayout): 
    def __init__(self): 
     GridLayout.__init__(self, cols=2, rows = 2); 
     self.add_widget(SubGrids()); 
     self.add_widget(SubGrids()); 
     self.add_widget(SubGrids()); 
     self.add_widget(SubGrids()); 
class Example(App): 
    def build(self): 
     return Grids() 

if __name__ == '__main__': 
    x = Example(); 
    x.run(); 

これはアイデアを与える願っています。

enter image description here

+1

ありがとう、これは私の問題を解決しました。メイングリッドにサブグリッドを追加することは考えていませんでした。しかしこれは理にかなっています。私は将来的に私の質問をより明確に提示します! – trashtatur

関連する問題