【问题标题】:python kivy widgets stacking in the bottompython kivy小部件堆叠在底部
【发布时间】:2018-11-25 10:47:30
【问题描述】:

我有一个代码,按下按钮应创建一个列表并将其放入窗口内的小部件列表中 但无论我选择哪种布局以及如何创建它们(指定 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'

小部件如何堆叠:

【问题讨论】:

  • 请分享您对menu_list_scroll的kv语言定义。
  • 如果这对我有帮助,我会这样做我真的很困惑
  • 用 StackLayout 替换 RelativeLayout
  • 我也尝试了很多东西,但如果它帮助我注意到它并不是所有小部件都在同一个地方看起来他们都“尝试”在同一个地方在我添加的图片中可以看到彼此重叠
  • 根据名称menu_list_scroll,您是否要创建一个可滚动的菜单列表?

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


【解决方案1】:

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

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

【讨论】:

  • 对不起,我不清楚它确实有效,所有小部件都成功进入屏幕功能:self.constr_widget 只返回特定小部件问题是 定位 他们都只是堆叠一个在彼此之上
  • @mgold 您仍然需要像我一样将布局放在屏幕内。布局内不是屏幕
【解决方案2】:

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

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

  1. 在kv文件中,将stacklayout的宽度设置为窗口的宽度,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'

输出

【讨论】:

  • 首先非常感谢您的努力,我真的很感激!我查看了您的示例并将其合并到代码中以尝试“重置”错误,但它的行为方式仍然相同,它们都可以创建正确的形状和大小,甚至屏幕也会相应变大,但唯一的问题是“堆叠”部分不会发生。我编辑了问题以反映我所做的更改并添加了与该窗口相关的每个部分,如果你能想到任何导致这种情况的原因,我很可能会高兴地跳起来,这个错误快把我逼疯了!
  • right_side.widthright_side.height 的值是多少?
  • row_instance.size_hint = (None, None)替换row_instance.size_hint = (1, None)
  • right_side 是双向浮动布局,可滚动窗口位于固定小部件(在启动时放置在那里)的固定高度下我尝试了您的建议并添加:row_instance.width=self.ids。 right_side.width 并没有帮助
  • 请张贴结果的打印屏幕。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-20
  • 1970-01-01
  • 2020-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多