2017-12-31 15 views
1

이것은 내 첫 번째 게시물이지만 가능한 한 자세히 분석하려고 노력할 것입니다. 그래서 내 응용 프로그램은 파이썬에 있고 그리드의 각 요소는 다섯 개의 추가 위젯과 가능성을 포함하는 Kivy의 그리드를 포함합니다. 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

안녕하십니까. 그리드마다 GridLayout 객체를 갖는 방법을 알고 싶습니다. 내부에 4/5 위젯이 있습니다. 예를 들어 4/5'Label' objects 일 수도 있습니다. 게시물에 질문을 명확히하거나 돋보이게 할 수 있습니다. – Arief

답변

0

내가 아이디어를 줄 수는 'subgrids'가 포함는 'subgrids'객체와의 주요 그리드 '객체를 생성 :

이 지금의 GUI의 전체 코드입니다. 이 두 객체는 ​​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