Created
March 11, 2017 20:08
-
-
Save quintopia/a13bf596ff7c6433a54253c1690f764b to your computer and use it in GitHub Desktop.
A problem with DragBehavior
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#:kivy 1.0.9 | |
<DragLabel>: | |
# Define the properties for the DragLabel | |
drag_rectangle: self.x, self.y, self.width, self.height | |
drag_timeout: 10000000 | |
drag_distance: 0 | |
border_width: 2 | |
font_size: "20dp" | |
text: "" | |
color: 1,0,0,1 | |
size_hint: None,None | |
size: self.texture_size[0]+2*self.border_width,self.texture_size[1]+2*self.border_width | |
canvas.before: | |
Color: | |
rgb: 0,0,0 | |
Rectangle: | |
pos: root.pos | |
size: root.size | |
Color: | |
rgb: 1,1,1 | |
Rectangle: | |
pos: root.x+root.border_width,root.y+root.border_width | |
size: root.width-2*root.border_width,root.height-2*root.border_width | |
Color: | |
rgb: 0,0,0 | |
FloatLayout: | |
# Define the root widget | |
DragLabel: | |
# size_hint: 0.25, 0.2 | |
pos: root.width/2-self.width/2,root.height/2-self.height/2 | |
text: 'Drag me' | |
color: 1,0,0,1 | |
DragLabel: | |
pos: root.width/2-self.width/2,root.height/2-self.height/2 | |
font_size: "23dp" | |
text: 'Drag me over or under?' | |
color: 0,1,0,1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from kivy.uix.label import Label | |
from kivy.app import App | |
from kivy.uix.floatlayout import FloatLayout | |
from kivy.uix.gridlayout import GridLayout | |
from kivy.uix.behaviors import DragBehavior | |
from kivy.lang import Builder | |
from kivy.core.window import Window | |
Window.clearcolor = (1, 1, 1, 1) | |
class ZDrag(DragBehavior): | |
def on_touch_down(self, touch): | |
parent = self.parent | |
if not hasattr(parent,'allow_more_drags'): parent.allow_more_drags = True | |
if parent.allow_more_drags: | |
success = super(ZDrag, self).on_touch_down(touch) | |
if success: | |
parent.allow_more_drags = False | |
parent.remove_widget(self) | |
parent.add_widget(self) | |
return success | |
return False | |
def on_touch_up(self, touch): | |
if self._drag_touch and self in [x() for x in touch.grab_list]: | |
self.parent.allow_more_drags = True | |
return super(ZDrag, self).on_touch_down(touch) | |
class DragLabel(ZDrag, Label): | |
pass | |
class DragLabelApp(App): | |
pass | |
if __name__ == '__main__': | |
DragLabelApp().run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is the fix:
def on_touch_up(self, touch): if self._drag_touch and self in [x() for x in touch.grab_list]: self.parent.allow_more_drags = True return super(ZDrag, self).on_touch_up(touch)
You had set return super(ZDrag, self).on_touch_up(touch) to return super(ZDrag, self).on_touch_down(touch)