【问题标题】:RecycleView with multiple colums with different class具有不同类的多列的 RecyclerView
【发布时间】:2021-11-14 13:36:42
【问题描述】:

我在这里遇到了一个大问题。我想在我的应用程序底部创建一个列表。我有一本适用于这个人解决方案的字典:How to put multiple columns into a kivy RecycleView?

但是,我想用按钮添加那个小部件。一切看起来都很好,但我的 ID 有问题。

请看一下,“FinalList”类找不到变量。

当您运行它时,单击“计算选择”它会添加另一个类,该类具有另一个名为“确认”的按钮。点击“确认”,应该在底部添加列表,但没有。

这是我的 main.py 代码;

from kivy.animation import Parallel
from kivy.app import App
from kivymd.app import MDApp
from kivy.uix.gridlayout import GridLayout
from kivy.uix.recycleview import RecycleView
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.stacklayout import StackLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.scrollview import ScrollView
from kivy.uix.button import Button
from kivy.uix.checkbox import CheckBox
from kivy.uix.label import Label
from kivy.uix.popup import Popup
from kivy.core.window import Window
from kivy.lang import Builder
from functools import partial
from kivy.properties import ObjectProperty, StringProperty, BooleanProperty

#Window.size=((640/1.5),(960/1.5))


items = [{'SP1': 'Artikelnummer', 'SP2': 'Name', 'SP3': 'Groesse'},
        {'SP1': '510001', 'SP2': 'Big Pump', 'SP3': '1.50 L'},
        {'SP1': '523001', 'SP2': 'Leonie Still', 'SP3': '1.50 L'},
        {'SP1': '641301', 'SP2': 'Cola Mix', 'SP3': '1.50 L'}
        ]



class MainWidget(Widget):

    cal2= ObjectProperty()
    cal4= ObjectProperty()
    
    def btn4(self):
        self.cal2 = MaterialS() #MaterialS is my class with different widget
        self.ids.scd.add_widget(self.cal2, index=0)  #scd is the id of my main widget, index =0 help me to insert to the bottom

    def btn8(self):
        self.cal4 = RV() **#HERE I CALL THE RV CLASS**
        self.ids.scd.add_widget(self.cal4, index=0) 


class SecondPage(ScrollView):
    pass

class Calculation(GridLayout):
    pass

class MaterialS(BoxLayout):
    def btn7(self,x):
        self.ids[x].disabled= True
    def btn9(self,x):
        self.ids[x].disabled= True


class FinalList(BoxLayout):
    pass

class RV(RecycleView):
    def __init__(self, **kwargs):
        super(RV, self).__init__(**kwargs)
        self.data = [{'spalte1_SP': str(x['SP1']), 'spalte2_SP': str(x['SP2']), 'spalte3_SP': str(x['SP3'])} for x in items]


class MyApp(MDApp):
    pass

MyApp().run()

这是我的.kv 文件;

MainWidget:
<MainWidget>:
    ScreenManager:
        id: scmanager
        size: root.width, root.height
        Screen:
            id: scndpage
            name: "second"
            SecondPage:
                Calculation:
                    id:scd            
                    cols:1 
                    height: self.minimum_height 
                    row_default_height: "70dp"
                    size_hint_y: None
                    spacing:"10dp"
                    canvas.before:
                        Rectangle:
                            pos: self.pos
                            size: self.size
                    Button:
                        id: cslect
                        text:"Calculate Selections"
                        on_press: app.root.btn4()


<MaterialS>:
    pading:"10dp"
    spacing:"10dp"
    size_hint: 1, None
    height: "250dp" 
    orientation:"vertical"
    BoxLayout:
        size_hint: 1, 1
        orientation:"horizontal"
        Label:  
            text:"cutter head material"
            color: 0,0,0,1



    Button:
        id: btnStd
        text:"Confirm"
        on_press: app.root.btn8()
        on_release: root.btn9("btnStd")


<Optionals>:
    BoxLayout:
        orientation: "vertical"

    Button:
        id: btnStd
        text:"Confirm"
        on_press: app.root.btn8()
        on_release: root.btn9("btnStd")



<FinalList>:
    orientation: 'horizontal'
    spalte1_SP: 'spalte1'
    spalte2_SP: 'spalte2'
    spalte3_SP: 'spalte3'
    Label:
        id: SP1
        text: app.root.spalte1_SP **#HERE IS THE MAIN PROBLEM**
    Label:
        id: SP2
        text: app.root.spalte2_SP **#HERE IS THE MAIN PROBLEM**
    Label:
        id: SP3
        text: app.root.spalte3_SP **#HERE IS THE MAIN PROBLEM**


<RV>:
    viewclass: 'FinalList'
    RecycleBoxLayout:
        default_size: None, dp(20)
        default_size_hint: 1, None
        size_hint_y: None
        height: self.minimum_height
        orientation: 'vertical'

【问题讨论】:

    标签: python kivy widget ids recycleview


    【解决方案1】:

    您只需要更正对FinalList 属性的引用。像这样:

    <FinalList>:
        orientation: 'horizontal'
        spalte1_SP: 'spalte1'
        spalte2_SP: 'spalte2'
        spalte3_SP: 'spalte3'
        
        Label:
            id: SP1
            text: root.spalte1_SP #**#HERE IS THE MAIN PROBLEM**
        Label:
            id: SP2
            text: root.spalte2_SP #**#HERE IS THE MAIN PROBLEM**
        Label:
            id: SP3
            text: root.spalte3_SP #**#HERE IS THE MAIN PROBLEM**
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多