是否可以在 kivy GridLayout 中为特定小部件指定特定网格
问题描述
我一直在使用 PyQt,我知道如果我们使用QGridLayout 在 PyQt 中,我们可以在特定的网格中指定特定的小部件.
I have been using PyQt since a long time and i know that if we use QGridLayout in PyQt we can specify a particular widget at a particular grid.
例如
grid = QtGui.QGridLayout()
grid.setSpacing(10)
grid.addWidget(self.Banner,0,0,1,4,QtCore.Qt.AlignTop | QtCore.Qt.AlignCenter)
grid.addWidget(self.RefNo,1,0,1,2)
grid.addWidget(self.RefNoText,1,2,1,2)
其中 self.banner
、self.Refno
和 self.RefNoText
是一些小部件.在这种情况下,我们可以指定小部件,然后指定行号.对于它和列号.为了它.Kivy 有可能做到这一点吗?
where self.banner
, self.Refno
and self.RefNoText
are some widgets. As in this case we can specify widget, then row no. For it and column no. For it. Is it possible in Kivy to do that?
我在 Kivy 中尝试过,但似乎在 Kivy 中我们必须在开始时指定行和列,然后在添加它们自己排列的小部件时?
I tried in Kivy but it seems that in Kivy we have to specify rows and columns in starting and then on adding widgets they are arranged by themselves?
有什么想法吗?
解决方案
在 Kivy 中,当我们将小部件添加到 GridLayout 时,我们只需指定行、列或两者.
In Kivy, when we add widgets to a GridLayout, we just specify either the rows, columns or both.
然后我们将定位留给 Kivy,它会根据我们的规范自动填充它.
We then leave the positioning to Kivy which automatically fills it as per our specifications.
查看 GridLayout 的官方文档
有一个非常好的 .gif 文件,它解释了小部件在 GridLayout 中的排列方式.
There is a really nice .gif which explains how widgets are arranged in a GridLayout.
所以,您的问题的答案是否定的.我们无法指定要放置小部件的位置,但是,我们所能做的就是以正确定位的方式将小部件添加到 GridLayout.
So, the answer to your question is NO. We cannot specify where we want to place a widget, however, all that we can do is add widgets to the GridLayout in such a manner that it positions it correctly.
最初,我发现很难让小部件占用比一个单元格更多的空间,如下所示:
Initially, I found difficulties in making a widget occupy more space than once cell, like this:
<2 cells><1cell>
+--------+-----+
|Big | |
|Widget | |
+--------+-----+
| | | |
| | | |
+---+----+-----+
但是,这可以通过将整行设为 BoxLayout 来解决.像这样:
However, this can be solved making the entire row a BoxLayout. Like so:
<1 big BoxLayout>
+--------+-----+
|Big | |
|Widget | |
+--------+-----+
| | | |
| | | |
+---+----+-----+
<1 more BoxLayout>
这样,每个 BoxLayout 只占用一个单元格.可以通过 BoxLayout 自定义小部件的间距,然后放置在 Grid 中.
This way, each BoxLayout occupies only one cell. The spacing of the widgets can be customized with the BoxLayout and then be placed in the Grid.
下面是如何在 Kv 中做到这一点:
Here's how to do that in Kv:
<MyGrid>
cols=1 # Actually, you can just use a vertical box
BoxLayout:
BigWidget:
# Properties
SmallWidget:
# Properties
BoxLayout:
SmallWidget:
# Properties
SmallWidget:
# Properties
SmallWidget:
# Properties
Kivy 会自动用 1 列填充 Grid
Kivy will automatically fill the Grid with 1 column
因此,通过嵌套布局,您可以获得所需的输出.
So, by nesting layouts you can get the desired output.
相关文章