Kivy 布局方形堆叠

Kivy Layout square Stacking

我正在制作一个类似于 exel 的程序。
然后我运行陷入了一个问题。
StackLayout 堆叠良好,但由于输入的大小是静态的,因此在某些情况下会留空 space。

我尝试像 size=(self.width/5, self.height/5) 那样做。
如果现在有人看到 exel 的外观,因为屏幕有更多输入,这就是我使用 ScrollLayout 的原因,我只想在一行中输入 5 个输入(用户可以在显示设置中更改它(我会这样做,然后创建 [=31 的其余部分) =]))

这是我目前尝试过的方法。
main.py:

from kivy.metrics import dp
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.stacklayout import StackLayout
from kivy.uix.textinput import TextInput


class EditorGrid(BoxLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        size = dp(self.width / 5)
        print(size)
        for i in range(0, 100):
            b = TextInput(text=str(i + 1), size_hint=(None, None), size=(dp(size), dp(size)))
            self.add_widget(b)

    def on_size(self, *args):
        self.size = dp(self.width/5)
        print(self.size)
        pass


class pxApp(App):
    pass


if __name__ == "__main__":
    pxApp().run()

kivy 文件:

Scrolling:

<Scrolling@ScrollView>
    do_scroll_x: True
    do_scroll_y: True
    EditorGrid:
        size_hint: 1, None
        height: self.minimum_height

<EditorGrid>:

下一个问题是 init window size 是 100x100.

我建议在 kv 脚本下绑定时使用 setters。如果你想在 python 中应用它,你可以为每个按钮使用一个绑定函数。

b = TextInput(.......) #omit size_hint and size attributes
#the following function is called every time EditorGrid(self) size_hint changes when the function is bind
#The function could be defined in a outer scope.
#Look at kivy callback functions to better understand the syntax
binding_function = lambda ins, *a: setattr(ins, 'size_hint', tuple((dim/5 for dim in self.size_hint))
b.bind(size_hint=binding_function)