是否可以在kivy GridLayout中为特定小部件指定特定网格 [英] Is it possible in kivy GridLayout to specify a particular grid for a particular widget

查看:183
本文介绍了是否可以在kivy GridLayout中为特定小部件指定特定网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很长一段时间以来,我一直在使用 PyQt ,我知道如果我们使用在PyQt中 QGridLayout ,我们可以在特定网格处指定特定窗口小部件.

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.bannerself.Refnoself.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列填充网格

Kivy will automatically fill the Grid with 1 column

因此,通过嵌套布局,您可以获得所需的输出.

So, by nesting layouts you can get the desired output.

这篇关于是否可以在kivy GridLayout中为特定小部件指定特定网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆