简体   繁体   English

python kivy小部件堆叠在底部

[英]python kivy widgets stacking in the bottom

I have a code that a button press should create a list and put it in list of widgets inside a window but no matter which layout I select and how I create them(specify pos or not) they are always created fine but all stack at the bottom of the screen 我有一个代码,按下按钮应该创建一个列表,并将其放在窗口内的小部件列表中,但是无论我选择哪种布局以及如何创建它们(指定pos),它们总是可以很好地创建,但是所有堆栈都位于屏幕底部

custom kv widget: 自定义kv小部件:

<item_widget@FloatLayout>
    Screen:
        size_hint:1,None
        height:25
        id:item_space
        color:18/256,47/256,82/256,0.4
        canvas.before:
            Color:
                rgba: self.color
            Rectangle:
                pos: self.pos
                size: self.size
        Label:
            id:description
            markup:True
            on_ref_press:root.open_hyperlink(args[1])
            text_size : self.size
            shorten:True
            shorten_from:'right'
            padding_x:4
            size_hint:None,1
            width:item_space.width-92
            pos:self.x,self.y
            valign:'center'
        Label:
            id:diameter
            text_size : self.size
            width:25
            halign:'center'
            size_hint:None,1
            pos:item_space.width-88,self.y
            valign:'center'
        Label:
            id:pitch
            text_size : self.size
            width:25
            halign:'center'
            size_hint:None,1
            pos:item_space.width-59,self.y
            valign:'center'
        Label:
            id:price_value
            text_size : self.size
            width:30
            halign:'center'
            size_hint:None,1
            pos:item_space.width-30,self.y
            valign:'center'

and this function triggers to create all the widgets in a list: 并且此函数触发以创建列表中的所有小部件:

def put_items_inscroll(self,item_list):
    i=0
    self.ids.menu_list_scroll.clear_widgets()
    for dict in item_list:
        self.ids.menu_list_scroll.add_widget(self.constr_widget(dict))
        i+=1

constr_widget definition: constr_widget定义:

def constr_widget(self,item,y=0):
    row_instance=item_widget()
    row_instance.ids.description.text='[ref='+item['link']+']'+str(item['description'])+'[/ref]'
    row_instance.ids.diameter.text=str(item['diameter'])
    row_instance.ids.pitch.text=str(item['pitch'])
    row_instance.ids.price_value.text=str(item['calc_valPerProp'])
    row_instance.size_hint = (1, None)
    row_instance.height=25
    return row_instance

Im really stuck with this and nothing i did changed it if Im only putting one widget it goes fine to the top but if I start putting them all they stack in the bottom 我真的坚持了这一点,如果我只将一个小部件放在顶部就可以了,但我没有做任何更改,但是如果我开始将它们全部放在底部,

screen definition: 屏幕定义:

    ScrollView:
        id:menu_list_scroll_window
        bar_width: 10
        effect_cls: "ScrollEffect"
        scroll_type: ['bars']
        size_hint: (None, None)
        size: (right_side.width, right_side.height-50)

        StackLayout:
            id: menu_list_scroll
            spacing: 5
            size_hint_y: None
            width: right_side.width
            height: self.minimum_height
            orientation: 'rl-tb'

how the widgets are stacking: 小部件如何堆叠: 在此处输入图片说明

You need to put the layout inside the screen. 您需要将布局放在屏幕内。
Like in this example: 像这个例子:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button

KV = """
ScreenManager:
    Screen:
        name: 'first'
        BoxLayout:
            Label:
                text: "Test label screen1"
            Button:
                text:'go to other'
                on_press: root.current = 'other'

    Screen:
        name: 'other'
        on_enter: mybox.add_labels()
        BoxLayout:
            orientation: "vertical"
            MyBox:
                id: mybox
            Button:
                size_hint: 1, 0.2
                text:'go to first'
                on_press: root.current = 'first'
"""

class MyBox(BoxLayout):

    def add_labels(self):
        self.clear_widgets()
        for i in range(10):
            self.add_widget(Button(text=str(i)))


class MyApp(App):

    def build(self):
        return Builder.load_string(KV)

MyApp().run()

Menu List: ScrollView + StackLayout - orientation='rl-tb' 菜单列表:ScrollView + StackLayout-orientation ='rl-tb'

To create a scrollable stack of menu list with orientation from right to left, and then top to bottom, 要创建菜单列表的可滚动堆栈,其方向从右到左,然后从上到下,

  1. In kv file, set the stacklayout's width to window's width, width: Window.width 在kv文件中,将stacklayout的宽度设置为窗口的宽度,即width: Window.width
  2. In Python code, set the size_hint=(None, None) for the widgets that will be added dynamically. 在Python代码中,为将动态添加的小部件设置size_hint=(None, None)

Please refer to the example for details. 有关详细信息,请参阅示例。

Note 注意

The attribute, orientation: 'vertical' is not a valid orientation for StackLayout. orientation: 'vertical'属性orientation: 'vertical'不是StackLayout的有效方向。

Stack Layout » orientation 堆栈布局»方向

 orientation 

Orientation of the layout. 布局方向。

orientation is an OptionProperty and defaults to 'lr-tb'. 方向是OptionProperty,默认为'lr-tb'。

Valid orientations are 'lr-tb', 'tb-lr', 'rl-tb', 'tb-rl', 'lr-bt', 'bt-lr', 'rl-bt' and 'bt-rl'. 有效方向为'lr-tb','tb-lr','rl-tb','tb-rl','lr-bt','bt-lr','rl-bt'和'bt-rl' 。

Example

main.py main.py

from kivy.app import App
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.button import Button


class RootWidget(RelativeLayout):

    def __init__(self, **kwargs):
        super(RootWidget, self).__init__(**kwargs)

        for i in range(100):
            btn = Button(text=str(i), size_hint=(None, None), width=40 + i * 5, height=90)
            self.ids.menu_list_scroll.add_widget(btn)


class TestApp(App):
    title = "Kivy Scrollable StackLayout Orientation='rl-tb' Demo"

    def build(self):
        return RootWidget()


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

test.kv 测试文件

#:kivy 1.10.0
#:import Window kivy.core.window.Window

<RootWidget>:

    ScrollView:
        bar_width: 10
        bar_color: 0, 1, 0, 1   # green
        bar_inactive_color: 1, 0, 0, 1   # red
        effect_cls: "ScrollEffect"
        scroll_type: ['bars']
        size_hint: (1, None)
        size: (Window.width, Window.height)

        StackLayout:
            id: menu_list_scroll
            spacing: 5
            size_hint_y: None
            width: Window.width
            height: self.minimum_height
            orientation: 'rl-tb'

Output 输出量

图01

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM