【问题标题】:adding multiple widgets to a scrollview将多个小部件添加到滚动视图
【发布时间】:2017-10-11 00:18:12
【问题描述】:

我正在尝试获取一个可滚动的小部件表格。每行都有不同的输入(此处由 TextInput 给出),但我的行数 (12) 比屏幕空间多得多,因此我将其放在 ScrollView 中。

问题在于下面的代码我收到以下错误。

 kivy.uix.widget.WidgetException: Cannot add <kivy.uix.boxlayout.BoxLayout object at 0x10e0abf30>, it already has a parent <kivy.uix.floatlayout.FloatLayout object at 0x10e0abfa0>

我的猜测是因为您不能在不给它们所有唯一 ID 的情况下将多个小部件添加到布局中?我不知道为什么main.add_widget(row_layout) 不允许您根据需要进行多次迭代?

.kv

ScrollView:
    do_scroll_x: False
        MyWidget:

我的小部件如下所示:

within .py

class MyWidget(BoxLayout):

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

    col1 = TextInput()
    col2 = TextInput()
    col3 = TextInput()
    col4 = TextInput()
    col5 = TextInput()
    col6 = TextInput()
    col7 = TextInput()
    col8 = TextInput()
    cols = [col1,col2,col3,col4,col5,col6,col7,col8]

    row_layout = BoxLayout(orientation='horizontal')
    print("Constructing row...")
    for col in cols:
        print col
        row_layout.add_widget(col)

    print("Iterating through rows...")
    main = FloatLayout(orientation='vertical')
    for row in range(12):
        print("adding...",row)
        main.add_widget(row_layout)

    self.add_widget(main)

【问题讨论】:

    标签: python-3.x scrollview kivy


    【解决方案1】:

    经过进一步的修改,我解决了这个问题。看来您需要为每一行创建一个新实例。

    class MyWidget(BoxLayout):
    
    def __init__(self, **kwargs):
        super(MyWidget, self).__init__(**kwargs)
    
        layout = GridLayout(cols=1,orientation='vertical',size_hint_y=None)
    
        layout.bind(minimum_height=layout.setter('height'))
    
        for row in range(24):
            col1 = TextInput()
            col2 = TextInput()
            col3 = TextInput()
            col4 = TextInput()
            col5 = TextInput()
            col6 = TextInput()
            col7 = TextInput()
            col8 = TextInput()
            cols = [col1,col2,col3,col4,col5,col6,col7,col8]
            row_layout = BoxLayout(orientation='horizontal', width=800,height=40,size_hint=(None, None))
    
            for col in cols:
                row_layout.add_widget(col)
    
            layout.add_widget(row_layout)
    
        root = ScrollView(do_scroll_x=False)
        root.add_widget(layout)
        self.add_widget(root)
    

    相关的kivy条目很简单:

    .kv
    MyWidget:
    

    【讨论】:

      猜你喜欢
      • 2020-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-25
      • 1970-01-01
      相关资源
      最近更新 更多