Skip to main content
 首页 » 编程设计

python之Kivy BoxLayout之将小部件移动到顶部

2025年05月04日10unruledboy

我让所有的小部件都相对于彼此调整大小和位置。如果我在 kv 代码的底部添加一个“标签:”,它会将所有内容移到顶部。但这不可能是做到这一点的“正确”方式。我错过了什么?

import kivy 
kivy.require('1.9.0') 
 
from kivy.app import App 
from kivy.uix.button import Button 
from kivy.uix.textinput import TextInput 
from kivy.uix.boxlayout import BoxLayout 
from kivy.lang import Builder 
 
Builder.load_string(''' 
<Controller>: 
    BoxLayout: 
        orientation: 'vertical' 
        padding: 20 
        spacing: 20 
        TextInput: 
            hint_text: 'Feed Name' 
            multiline: False 
            size_hint: (0.75, None) 
            height: 30 
            pos_hint: {'center_x': 0.5} 
        TextInput: 
            hint_text: 'Feed URL' 
            multiline: True 
            size_hint: (0.75, None) 
            height: 68 
            pos_hint: {'center_x': 0.5} 
        Button: 
            text: 'Add Feed' 
            padding: (10, 10) 
            height: 30 
            size_hint: (None, None) 
            pos_hint: {'center_x': 0.5} 
''') 
 
class Controller(BoxLayout): 
    pass 
 
class PodcastApp(App): 
      def build(self): 
          return Controller(info='Hello world') 
 
if __name__ == '__main__': 
    PodcastApp().run() 

请您参考如下方法:

来自 BoxLayout docs :

padding Added in 1.0.0 Padding between layout box and children: [padding_left, padding_top, padding_right, padding_bottom].

padding also accepts a two argument form [padding_horizontal, padding_vertical] and a one argument form [padding].

Changed in version 1.7.0: Replaced NumericProperty with VariableListProperty.

padding is a VariableListProperty and defaults to [0, 0, 0, 0].

如果您希望它的子项被推向顶部,请尝试在框布局的底部添加填充。

例如给填充值

[20, 20, 20, 'new bottom padding here']