Kivy翻倍了RecycleView

时间:2019-04-05 04:19:17

标签: python kivy

有人知道这是什么原因吗?我的Kivy RecycleView在可编辑的代码后面有一个奇怪的静态版本-无法以任何方式选择或更改它。这让我觉得我可能拥有所有Kivy小部件的重复版本?我不愿在将所有代码粘贴到api时粘贴我的所有代码,并在应用程序本身中包含凭据信息以及许多个人信息。

DoubledRecycleView


class SelectableRecycleBoxLayout(FocusBehavior, LayoutSelectionBehavior,
                                 RecycleBoxLayout):
    ''' Adds selection and focus behaviour to the view. '''


class SelectableLabel(RecycleDataViewBehavior, Label):
    ''' Add selection support to the Label '''
    index = None
    selected = BooleanProperty(False)
    selectable = BooleanProperty(True)

    def refresh_view_attrs(self, rv, index, data):
        ''' Catch and handle the view changes '''
        self.index = index
        return super(SelectableLabel, self).refresh_view_attrs(
            rv, index, data)

    def on_touch_down(self, touch):
        ''' Add selection on touch down '''
        if super(SelectableLabel, self).on_touch_down(touch):
            return True
        if self.collide_point(*touch.pos) and self.selectable:
            return self.parent.select_with_touch(self.index, touch)

    def apply_selection(self, rv, index, is_selected):
        ''' Respond to the selection of items in the view. '''
        self.selected = is_selected
        if is_selected:
            print("selection changed to {0}".format(rv.data[index]))
        else:
            print("selection removed for {0}".format(rv.data[index]))



class RV(RecycleView):

    def __init__(self, **kwargs):
        super(RV, self).__init__(**kwargs)
        self.data = [{'text': str(x)} for x in range(10)]

class GuiApp(App):
    theme_cls = ThemeManager()
    theme_cls.theme_style = 'Dark'
    previous_date = ''
    previous_date2 = ''
    StartDate = ObjectProperty("Start Date")
    EndDate = ObjectProperty("End Date")



    def build(self):
        self.pickers = Factory.AnotherScreen()
        presentation = Builder.load_file("gui.kv")
        return presentation


这是我的奇异果:


            RV:
                id: rv
                viewclass: 'SelectableLabel'
                SelectableRecycleBoxLayout:
                    default_size: None, dp(56)
                    default_size_hint: 1, None
                    size_hint_y: None
                    height: self.minimum_height
                    orientation: 'vertical'
                    multiselect: True
                    touch_multiselect: True


<SelectableLabel>:
    # Draw a background to indicate selection
    canvas.before:
        Color:
            rgba: (.4, 0.4, .4, .3) if self.selected else (.2, .2, .2, 1)
        Rectangle:
            pos: self.pos
            size: self.size

我希望这是足够的信息,至少可以让我知道在哪里查找。我也正在努力更新RecycleView中的数据,但这可能是因为此问题?我可能有两个不同的小部件实例正在运行,但是我不确定这是怎么可能的。

1 个答案:

答案 0 :(得分:0)

根本原因-看过双打

双重作用是由于按文件名约定并使用Builder加载kv文件。

例如: :使用Builder.load_file('gui.kv'),并且您的kv文件名为gui.kv

摘要

class GuiApp(App):
    ...

    def build(self):
        self.pickers = Factory.AnotherScreen()
        presentation = Builder.load_file("gui.kv")
        return presentation

解决方案

有两种解决方法。

方法1

将App类从GuiApp()重命名为TestApp()

方法2

只要存在根规则,例如,无需在App类中返回任何内容。 kv文件中的BoxLayout:

删除以下内容:

  • presentation = Builder.load_file("gui.kv")
  • return presentation

参考

Kv language » How to load KV

  

有两种方法可以将Kv代码加载到您的应用程序中:

     

按名称惯例:

     

Kivy在以下位置查找与您的App类同名的Kv文件   小写字母,如果以“ App”结尾则减去“ App”,例如:

MyApp -> my.kv
     

如果此文件定义了Root Widget,它将被附加到应用程序的   根属性,并用作应用程序小部件树的基础。

     

按照构建器惯例:

     

您可以告诉Kivy直接加载字符串或文件。如果这个字符串   或文件定义了根窗口小部件,它将由以下方法返回:

Builder.load_file('path/to/file.kv')
     

或:

Builder.load_string(kv_string)

示例

main.py

from kivy.app import App


class GuiApp(App):

    def build(self):
        self.pickers = None


if __name__ == "__main__":
    GuiApp().run()

gui.kv

#:kivy 1.11.0

Button:
    text: 'Hello Kivy'
    font_size: 50

输出

Result

相关问题