从ListVi拖放Kivy

2024-04-20 11:58:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试设置一个ListView以便

1)选择后,该选择将从列表视图中删除

2)创建一个散点图,其中包含选定内容的标签

3)使用相同的单击,散布在屏幕上拖动

4)释放单击后,散点将被删除。在

我在tkinter做的,我正试着把这个转换成Kivy。大多数情况下,这是非常简单的,但是我遇到了几个问题。我的第一个问题是获取ListView的选择。ListView的on_touch_down事件在ListView适配器的on_selection_change事件之前被触发,所以如果我绑定到on_touch_down,我会得到之前的选择,而不是当前的选择。第二个问题是拖散。目标是让用户在一次点击中,从列表视图中选择一个,在屏幕上显示一个散点并将其拖过屏幕,然后在释放单击时将其删除。我尝试在下面绑定到ListView的touch.grab()的方法中使用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)

但是当我点击ListView时,我得到了一个TypeError: cannot create weak reference to 'weakproxy' object错误,尽管我的.kv文件中有keyScatter: keyScatter.__self__,而{}是{}的id。在

这两个问题都有好的解决办法吗?在


Tags: selfview视图内容列表屏幕on事件
1条回答
网友
1楼 · 发布于 2024-04-20 11:58:27

对于任何试图从ListView进行拖放操作的人来说,有一个好消息:解决方案已经存在。为了解决这个问题,我使用了绑定到ListView适配器的on_selection_change、ListView的on_touch_down和我用于拖放的散点的{}的方法,如下面的代码块中所示。在

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

相关问题 更多 >