猿问

Kivy 将 RecycleView 翻了一番

有谁知道是什么导致了这种情况?我的 Kivy RecycleView 在可编辑的后面有一个奇怪的静态版本 - 它不能以任何方式选择或更改。这让我觉得我可能有我所有 Kivy 小部件的重复版本?我很犹豫要不要粘贴我的所有代码,因为它会访问一个 api 并且在应用程序本身中包含凭据信息和许多个人信息。

双循环视图

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


大话西游666
浏览 297回答 1
1回答

忽然笑

根本原因 - 看到双打双倍是由于按文件名约定加载 kv 文件并使用Builder.例如:使用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解决方案该问题有两种解决方案。方法一将 App 类重命名GuiApp()为TestApp()方法二只要有根规则,例如BoxLayout:在 kv 文件中,就不需要在 App 类中返回任何内容。删除以下内容:presentation = Builder.load_file("gui.kv")return presentation参考KV语言 » 如何加载KV有两种方法可以将 Kv 代码加载到您的应用程序中:按命名约定:Kivy 查找与您的 App 类同名的小写 Kv 文件,如果以“App”结尾,则减去“App”,例如:MyApp -> my.kv如果此文件定义了一个根小部件,它将附加到应用程序的根属性并用作应用程序小部件树的基础。按建造者约定:您可以告诉 Kivy 直接加载字符串或文件。如果此字符串或文件定义了根小部件,它将由以下方法返回:Builder.load_file('path/to/file.kv')或者:Builder.load_string(kv_string)例子主文件from kivy.app import Appclass GuiApp(App):    def build(self):        self.pickers = Noneif __name__ == "__main__":    GuiApp().run()gui.kv#:kivy 1.11.0Button:    text: 'Hello Kivy'    font_size: 50输出
随时随地看视频慕课网APP

相关分类

Python
我要回答