【问题标题】:Kivy Layout square StackingKivy 布局方形堆叠
【发布时间】:2021-07-17 15:59:43
【问题描述】:

我正在制作一个类似于 exel 的程序。
然后我遇到了一个问题。
StackLayout 的堆栈很好,但由于输入的大小是静态的,因此在某些情况下会留下空白。

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

这是我现在尝试过的。
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 中的下一个问题是窗口大小为 100x100。

【问题讨论】:

  • 您的kivy 文件和您的main.py 相同吗?
  • @John Anderson 抱歉,复制了错误的内容。

标签: python python-3.x kivy kivy-language


【解决方案1】:

我建议在 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)

【讨论】:

  • 我完全不明白如何实现它...我是初学者,我完全不知道在说什么。
  • 查找 python lambda functions、setattr 和 kivy 的 bind。这些分别是 python 和 kivy 的重要功能。如果对 setter 方法感到困惑,请查看示例并尝试将其应用于您的实例。
  • 那么我把它放在哪里了?
  • 第一行与您的代码类似。用我的代码的 TextInput 建议和以下代码替换您的 TextInput。
  • 对吗?它打印“无效语法错误”代码链接:gist.github.com/comand100vip/65f4ebf4e65ab293d1a8c632e0924bda
猜你喜欢
  • 2023-03-22
  • 2023-04-04
  • 2012-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多