問題描述
我一直在使用 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.
這篇關于是否可以在 kivy GridLayout 中為特定小部件指定特定網格的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!