为了账号安全,请及时绑定邮箱和手机立即绑定

Kivy 将 RecycleView 翻了一番

Kivy 将 RecycleView 翻了一番

大话西游666 2021-12-29 19:44:56
有谁知道是什么导致了这种情况?我的 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
查看完整描述

1 回答

?
忽然笑

TA贡献1806条经验 获得超5个赞

根本原因 - 看到双打

双倍是由于按文件名约定加载 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 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

输出

//img1.sycdn.imooc.com//61cc4b8b00012ed407990625.jpg

查看完整回答
反对 回复 2021-12-29
  • 1 回答
  • 0 关注
  • 296 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号