<i id='PNypQ'><tr id='PNypQ'><dt id='PNypQ'><q id='PNypQ'><span id='PNypQ'><b id='PNypQ'><form id='PNypQ'><ins id='PNypQ'></ins><ul id='PNypQ'></ul><sub id='PNypQ'></sub></form><legend id='PNypQ'></legend><bdo id='PNypQ'><pre id='PNypQ'><center id='PNypQ'></center></pre></bdo></b><th id='PNypQ'></th></span></q></dt></tr></i><div id='PNypQ'><tfoot id='PNypQ'></tfoot><dl id='PNypQ'><fieldset id='PNypQ'></fieldset></dl></div>

      <tfoot id='PNypQ'></tfoot>

      <small id='PNypQ'></small><noframes id='PNypQ'>

        • <bdo id='PNypQ'></bdo><ul id='PNypQ'></ul>

      1. <legend id='PNypQ'><style id='PNypQ'><dir id='PNypQ'><q id='PNypQ'></q></dir></style></legend>

        小部件在 GridLayout 中的位置

        时间:2023-06-06

          <legend id='71DGV'><style id='71DGV'><dir id='71DGV'><q id='71DGV'></q></dir></style></legend>
          <tfoot id='71DGV'></tfoot>
            <i id='71DGV'><tr id='71DGV'><dt id='71DGV'><q id='71DGV'><span id='71DGV'><b id='71DGV'><form id='71DGV'><ins id='71DGV'></ins><ul id='71DGV'></ul><sub id='71DGV'></sub></form><legend id='71DGV'></legend><bdo id='71DGV'><pre id='71DGV'><center id='71DGV'></center></pre></bdo></b><th id='71DGV'></th></span></q></dt></tr></i><div id='71DGV'><tfoot id='71DGV'></tfoot><dl id='71DGV'><fieldset id='71DGV'></fieldset></dl></div>

                <tbody id='71DGV'></tbody>

                <bdo id='71DGV'></bdo><ul id='71DGV'></ul>

                <small id='71DGV'></small><noframes id='71DGV'>

                1. 本文介绍了小部件在 GridLayout 中的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试创建一个小部件网格,每个单元格"小部件都有一个矩形,我可以稍后更改颜色.当我使用 grid.add_widget(Button(text=str(i))) 行运行下面的代码时,按钮会填充窗口(如文档中所示).但是,当我在 grid.add_widget(Cell()) 中使用我的 Cell 小部件时,所有小部件都聚集在左下角(position=[0,0], size=[100,100]).我发现了这个问题 Kivy: Add rawWidget 为 GridLayout(与 Image、Button 等相反) 并将我的 Cell 类更改为从 Layout 而不是 Widget 继承但这无济于事.关于我做错了什么的任何想法?我想要的是一个彩色矩形网格.

                  I'm trying to create a grid of widgets with each "cell" widget having a rectangle I can change the color of later. When I run the code below with the line grid.add_widget(Button(text=str(i))) the buttons fill the window (as in the docs). However, when I use my Cell widget as in grid.add_widget(Cell()) all the widgets are bunched in the lower left corner (position=[0,0], size=[100,100]). I found this question Kivy: Add raw Widget to GridLayout (as opposed to Image, Button, etc) and changed my Cell class to inherit from Layout instead of Widget but that didn't help things. Any ideas on what I'm doing wrong? What I'd like is a grid of colored rectangles.

                  版本

                  [INFO   ] [Kivy        ] v1.9.1
                  [INFO   ] [Python      ] v2.7.6 (default, Jun 22 2015, 17:58:13) 
                  

                  代码

                  from kivy.app import App
                  from kivy.core.window import Window
                  from kivy.uix.widget import Widget
                  from kivy.uix.button import Button
                  from kivy.uix.gridlayout import GridLayout
                  from kivy.uix.layout import Layout
                  from kivy.graphics import Rectangle, Color
                  
                  XSIZE=2
                  YSIZE=2
                  
                  class Cell(Layout):
                      def __init__(self, *args, **kwargs):
                          super(Cell, self).__init__(*args, **kwargs)
                          with self.canvas:
                              Rectangle(size=self.size, pos=self.pos)
                              Color((0.2, 0.2, 0.2, 1.0))
                  
                  class GameApp(App):
                      def build(self):
                          grid = GridLayout(rows=YSIZE, cols=XSIZE, size=Window.size)
                          for i in xrange(4):
                              print 'i={}'.format(i)
                  #            grid.add_widget(Cell())
                              grid.add_widget(Button(text=str(i)))
                          return grid
                  
                  if __name__ == '__main__':
                      GameApp().run()
                  

                  推荐答案

                  我将展示如何实现这一目标的示例.我将创建一个网格类,并向其中添加单元格小部件.但我只会使用一个画布(gridlayouts 画布).所以单元格类将包含一个 InstructionGroup,而不是制作多个画布.
                  为了以后能够设置每个单元格的大小、位置和颜色,这些属性必须是单元格类的属性.
                  属性将在 MyGridset_attributes 方法中设置.
                  首先,当应用程序运行时,您可以获得单元格的位置,因此我使用 Clock.schedule_once 方法.这将执行下一帧.
                  为了演示如何更改颜色,我创建了一个 Clock.schedule_interval,用随机颜色为单元格的颜色设置动画.

                  I will show an example on how you can achieve this. I will create a grid class, and add the cell widgets to it. But I will only use one canvas (the gridlayouts canvas). So the cell class will contain an InstructionGroup, rather than make multiple canvases.
                  To be able to set size, position and color of each cell later, those attributes must be attributes of the cell class.
                  The attributes will be set in MyGrid's set_attributes method.
                  First when the app is running, you can get the positions of the cells, hence I use the Clock.schedule_once method. That will execute the next frame.
                  And to demonstrate how to change the colors, I create a Clock.schedule_interval, to animate the colors of the cells with random colors.

                  下面是例子:

                  from kivy.app import App
                  from kivy.uix.widget import Widget
                  from kivy.uix.button import Button
                  from kivy.uix.gridlayout import GridLayout
                  from kivy.graphics import Rectangle, Color, InstructionGroup
                  from kivy.clock import Clock
                  
                  from random import uniform
                  
                  XSIZE=2
                  YSIZE=2
                  
                  class Cell(Widget):
                      def __init__(self, i, **kwargs):
                          super(Cell, self).__init__(**kwargs)
                          self.ig = InstructionGroup()
                          self.rect = Rectangle()
                          self.color = Color(0.2, 0.2, 0.2*i)
                          self.ig.add(self.color)
                          self.ig.add(self.rect)
                  
                  
                  
                  class MyGrid(GridLayout):
                      def __init__(self,**kwargs):
                          super(MyGrid,self).__init__(**kwargs)
                          self.rows=YSIZE
                          self.cols=XSIZE
                          for i in xrange(4):
                              self.add_widget(Cell(i))
                              self.canvas.add(self.children[0].ig)
                  
                          Clock.schedule_once(self.set_attributes)
                          Clock.schedule_interval(self.change_color,1)
                  
                      def set_attributes(self,dt):
                          for i in self.children:
                              i.rect.pos = i.pos
                              i.rect.size = i.size
                  
                      def change_color(self,dt):
                          for i in self.children:
                              i.color.rgb = (uniform(0.0,1.0),uniform(0.0,1.0),uniform(0.0,1.0))
                  
                  
                  class GameApp(App):
                      def build(self):
                          return MyGrid()
                  
                  
                  if __name__ == '__main__':
                      GameApp().run()
                  

                  这篇关于小部件在 GridLayout 中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Kivy:在python中添加的小部件中获取父级 下一篇:PyInstaller 中的 Kivy Garden - 试图跟踪导入

                  相关文章

                2. <legend id='2QLbK'><style id='2QLbK'><dir id='2QLbK'><q id='2QLbK'></q></dir></style></legend>
                3. <i id='2QLbK'><tr id='2QLbK'><dt id='2QLbK'><q id='2QLbK'><span id='2QLbK'><b id='2QLbK'><form id='2QLbK'><ins id='2QLbK'></ins><ul id='2QLbK'></ul><sub id='2QLbK'></sub></form><legend id='2QLbK'></legend><bdo id='2QLbK'><pre id='2QLbK'><center id='2QLbK'></center></pre></bdo></b><th id='2QLbK'></th></span></q></dt></tr></i><div id='2QLbK'><tfoot id='2QLbK'></tfoot><dl id='2QLbK'><fieldset id='2QLbK'></fieldset></dl></div>

                  <small id='2QLbK'></small><noframes id='2QLbK'>

                    <bdo id='2QLbK'></bdo><ul id='2QLbK'></ul>

                  <tfoot id='2QLbK'></tfoot>