python kivy小部件堆叠在底部

时间:2018-06-15 14:58:51

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

我有一个代码,按下按钮应创建一个列表并将其放在窗口内的小部件列表中 但无论我选择哪种布局以及如何创建它们(指定pos或不指定pos) 它们总是很好,但都堆叠在屏幕的底部

自定义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'

并且此函数触发创建列表中的所有小部件:

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定义:

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

我真的坚持这一点,我没有改变它 如果我只放一个小部件就可以了 但如果我开始将它们全部放在底部

屏幕定义:

    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'

小部件如何堆叠: enter image description here

2 个答案:

答案 0 :(得分:0)

您需要将布局放在屏幕内 就像在这个例子中一样:

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()

答案 1 :(得分:0)

菜单列表:ScrollView + StackLayout-direction ='rl-tb'

要创建菜单列表的可滚动堆栈,其方向是从右到左,然后从上到下,

  1. 在kv文件中,将堆栈布局的宽度设置为窗口的宽度width: Window.width
  2. 在Python代码中,为将动态添加的小部件设置size_hint=(None, None)

有关详细信息,请参阅示例。

注意

orientation: 'vertical'属性不是StackLayout的有效方向。

Stack Layout » orientation

orientation
     

布局的方向。

     

orientation是一个OptionProperty,默认为“ lr-tb”。

     

有效方向为“ lr-tb”,“ tb-lr”,“ rl-tb”,“ tb-rl”,“ lr-bt”,   “ bt-lr”,“ rl-bt”和“ bt-rl”。

示例

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'

输出

Img01