【问题标题】:Displaying different widget types in Recycleview Kivy在 Recycleview Kivy 中显示不同的小部件类型
【发布时间】:2020-08-06 21:40:22
【问题描述】:

我想知道是否可以在 kivy recycleview 中显示不同的小部件类型,我让它显示文本框,这很好,但可以在同一个回收视图中显示图像,还是我必须在顶部创建另一个 recycleview我现有的,这是我的代码和当前的应用程序快照,我目前的理解是你只能在回收视图中有一个视图类,我的是文本输入,那么如何将另一个视图类添加到同一个回收视图。

当前屏幕 https://i.stack.imgur.com/u8JpE.jpg

main.kv

   SelectableReportTextbox:



    size_hint:  None,None
    text_size : self.text_size
    size_hint_y: None
    font_size: self.height*0.2
    foreground_color: [1, 1, 1, 1]
    readonly: True
    background_color: (0.1, 0.3, 0.7, 0.7)
    padding_x: [30,30]
    font_name: 'C:\kivy_venv\Graphics\GIL_____.TTF'
    border: [50,50,50,50]







  ScreenTwo:
  




    canvas.before:


            Rectangle:
                    size:self.size           #100, 100
                    pos: self.pos
                    source: "C:\kivy_venv\Graphics\Jetfire back.png"


    RecycleView:

            do_scroll: True, True
            bar_width: 6
            size_hint: (None, None)
            id: scrlv
            size: (500, 500)
            pos_hint: {'center_x': .75, 'center_y': .64}
            scroll_y: 0
            multiline:True




            ProjectRV:

                    viewclass: 'SelectableReportTextbox'        # defines the viewtype for the data items.
                    orientation: "vertical"
                    scroll_type: ['bars', 'content']
                    scroll_wheel_distance: dp(114)
                    key_size: "height"

                    padding:1, 1
                    space_x: self.size[0]/3
                    id: rv
                    pos_hint: {'center_x': 0.32, 'center_y': 0.525}
                    bar_width: dp(25)
                    bar_color: (0.7, 0.1, 0.3, 0.7)
                    bar_inactive_color: (0.1, 0.1, 0.1 , 1)
                    scroll_y : 0





                    SelectableRecycleBoxLayout:


                            data : []
                            spacing : '5'
                            default_size_hint: 1, None
                            size_hint_y: None
                            height: self.minimum_height
                            multiselect: True
                            touch_multiselect: True
                            orientation: 'vertical'




                            SelectableRecycleBoxLayout:

                                    data : []
                                    color:(0, 0.7, 0.4, 0.4)
                                    spacing : '5'
                                    default_size_hint: 1, None
                                    size_hint_y: None
                                    height: self.minimum_height
                                    multiselect: True
                                    touch_multiselect: True
                                    orientation: 'vertical'

【问题讨论】:

    标签: python kivy


    【解决方案1】:

    您可以定义自己的viewclass 来显示您想要的任何内容。您的viewclass 必须具有与RecycleViewdata 中的键相对应的Properties

    这是一个简单的例子:

    from kivy.app import App
    from kivy.lang import Builder
    from kivy.uix.recycleview import RecycleView
    
    
    class RV(RecycleView):
        def __init__(self, **kwargs):
            super(RV, self).__init__(**kwargs)
    
            # set up the data
            self.data = [
                {'text': 'Some Text', 'image': 'dots.png'},
                {'text': 'more text', 'image': 'tester.png'}
                ]
    
    kv = '''
    <MyViewClass@BoxLayout>:
        # define the properties that appear in the data
        text: ''
        image: ''
        
        # define how the data is displayed
        Image:
            source: root.image
        Label:
            text: root.text
    RV:
        viewclass: 'MyViewClass'
        RecycleBoxLayout:
            padding: 10, 0, 10, 0
            size_hint_y: None
            height: self.minimum_height
            default_size: None, 40
            default_size_hint: 1, None
            orientation: 'vertical'
            spacing: 3
    '''
    
    class TestApp(App):
        def build(self):
            return Builder.load_string(kv)
    
    TestApp().run()
    

    【讨论】:

    • 感谢约翰,你总是很有帮助,你的例子很棒
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-03
    • 2017-10-28
    相关资源
    最近更新 更多