简体   繁体   English

Kivy从ListView拖放

[英]Kivy drag and drop from ListView

I'm attempting to set up a ListView so that 我正在尝试设置一个ListView

1) When selection is made, that selection is deleted from the ListView 1)做出选择后,该选择将从ListView中删除

2) A Scatter is created holding a label with the text of the selection 2)创建一个散点图,其中包含带有所选文本的标签

3) Using the same click, the Scatter is dragged across the screen 3)使用相同的单击,在屏幕上拖动散点图

4) The Scatter is deleted once the click is released. 4)释放点击后,便会删除散点图。

I did this in tkinter, and I'm trying to transition this to Kivy. 我是在tkinter上完成的,现在正在尝试将其转换为Kivy。 For the most part, this is pretty straightforward, but I've encountered a couple of issues. 在大多数情况下,这非常简单,但是我遇到了两个问题。 The first issue I have is getting the selection of the ListView. 我遇到的第一个问题是选择ListView。 The on_touch_down event of the ListView gets fired before the on_selection_change event of the ListView's adapter, so if I bind to on_touch_down , I get what the previous selection was, not the current one. on_touch_down ListView控件的事件得到之前触发on_selection_change ListView的适配器的情况下,所以如果我绑定到on_touch_down ,我得到了以前的选择是,不是当前。 The second issue is dragging the Scatter. 第二个问题是拖延分散。 The goal is for the user, in 1 click, to make a selection from the ListView, have a Scatter appear and drag it across the screen, and then have the Scatter be removed when the click is released. 目标是让用户单击1即可从ListView中进行选择,出现一个Scatter并将其拖到屏幕上,然后在释放单击时将Scatter删除。 I've tried to use touch.grab() in the following method that was bound to the ListView's on_touch_down 我试图在绑定到ListView的on_touch_down的以下方法中使用touch.grab()

def onPress(self, view, touch):
    if view.collide_point(touch.x, touch.y):
        self.floatLayout.add_widget(self.scatter)
        touch.grab(self.scatter)

But when I click on the ListView I get a TypeError: cannot create weak reference to 'weakproxy' object error, despite having keyScatter: keyScatter.__self__ in my .kv file, and keyScatter is the id for self.scatter . 但是,当我点击的ListView我得到一个TypeError: cannot create weak reference to 'weakproxy' object ,尽管有错误keyScatter: keyScatter.__self__在我.kv文件, keyScatter是为ID self.scatter

Is there a good fix for either issue? 有解决这两个问题的好方法吗?

To anyone who attempts to do drag and drop from a ListView, there is good news: a solution exists. 对于尝试从ListView进行拖放的任何人,都有一个好消息:存在解决方案。 To solve this problem, I used methods bound to the on_selection_change of the ListView's adapter, the on_touch_down of the ListView, and the on_touch_up of the Scatter that I was being used for the drag and drop, as in the following code block. 为了解决这个问题,我用绑定方法on_selection_change ListView的适配器,该on_touch_down ListView控件,而on_touch_up ,我是正在使用的拖放操作,如下面的代码块分散的。

self.listview.bind(on_touch_down=self.press)
self.scatter.bind(on_touch_up=self.release)
self.adapter.bind(on_selection_change=self.selectionChange)

def selectionChange(self, adapter):
    if adapter.selection: #Sometimes the selection was [], so a check doesn't hurt 
        names = adapter.data
        self.scatter.children[0].text = adapter.selection[0].text #My scatter has a label as it's first and only child. Here, I'm changing the label's text
        for j in adapter.data:
            if j == adapter.selection[0].text:
                break
        names.pop(names.index(j))
        self.listview.adapter.data = names
        if(hasattr(self.listview, '_reset_spopulate')): #This is used to reset the ListView
            self.listview._reset_spopulate()

def press(self, view, touch):
    if view.collide_point(touch.x, touch.y) and not touch.is_mouse_scrolling:
        self.scatter.center = touch.pos
        self.floatLayout.add_widget(self.scatter) #The scatter appears on the click
        self.scatter.on_touch_down(touch) #Needs to be called to get the scatter to be dragged

def release(self, scatter, touch):
    if scatter.collide_point(touch.x, touch.y) and touch.grab_current: #Because Kivy's on_touch_up doesn't work like I think it does

        #Do whatever you want on the release of the scatter

        self.floatLayout.remove_widget(self.scatter) #Remove the scatter on release

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM