避免占用整个屏幕的Kivy小部件

时间:2018-01-03 10:30:30

标签: python kivy

我创建了一个按钮'添加输入框'这将添加一个输入框 用户按下它,因为我不知道输入框的数量 用户可能需要。

这些输入框应垂直排列,以便添加每个输入框 将在前一个位置正下方的特定位置创建 不考虑size_hint(即使它是一个输入框,它 不应该覆盖屏幕。如果我告诉它在位置(0,10),它 不应超过那个位置。)

我尝试了BoxLayout,它创建的第一个InputBox覆盖了 整个屏幕。第二个占据屏幕的50%。第三个, 屏幕的1/3等等。

我使用下面的代码检查了FloatLayout:

class NextWindow(Screen):
def __init__(self, **kwargs):
    super(NextWindow, self).__init__(**kwargs)
    self.count_box = 1
    self.layout = FloatLayout()

def addInputBox(self, obj):
    inputBox = TextInput(multiline=False,size_hint=(0.2, 0.05),
                         pos_hint={'top': self.count_box})

    if self.count_box < 150:
        self.count_box = self.count_box + 10   #Changes the value of the position
        self.layout.add_widget(inputBox)       #so that the next InputBox is created

    else:                                      #under the previous one.
        pass #When 14 InputBoxes have been created

按下按钮 &#39;添加InputBox&#39;,改变self.count_box的值,使其值为 &#39;顶部&#39;每次调用后都会更改,以便下一个InputBox位于前一个InputBox之下。

这并没有占据整个屏幕。它只显示了第一个InputBox,但当&#39; Add InputBox&#39;时,屏幕上不显示后续的InputBox。被按下了。

1 个答案:

答案 0 :(得分:0)

这源于对pos_hint属性的理解不足。实现它的一种方法是忽略pos_hint并提供实际的位置属性,&#39; pos&#39; pos。 &#39; pos&#39;的价值每次点击“添加课程”后都会更改。按钮。

相关问题