Skip to content

Instantly share code, notes, and snippets.

@onslauth
Last active February 27, 2018 09:39
Show Gist options
  • Save onslauth/6aa0a1005caddff9c797fb4a9d5d4299 to your computer and use it in GitHub Desktop.
Save onslauth/6aa0a1005caddff9c797fb4a9d5d4299 to your computer and use it in GitHub Desktop.
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from kivy.uix.behaviors import DragBehavior
class DragLabel( DragBehavior, Label ):
pass
class DragPopup( DragBehavior, Popup ):
class DragPopup( DragBehavior, Popup ):
def on_touch_move(self, touch):
if self._get_uid('svavoid') in touch.ud or self._drag_touch is not touch:
return super(DragBehavior, self).on_touch_move(touch) or self._get_uid() in touch.ud
if touch.grab_current is not self:
return True
uid = self._get_uid()
ud = touch.ud[uid]
mode = ud['mode']
if mode == 'unknown':
ud['dx'] += abs(touch.dx)
ud['dy'] += abs(touch.dy)
if ud['dx'] > sp(self.drag_distance):
mode = 'drag'
if ud['dy'] > sp(self.drag_distance):
mode = 'drag'
ud['mode'] = mode
if mode == 'drag':
self.x += touch.dx
self.y += touch.dy
self.pos_hint = { 'x': ( self.x + touch.dx ) / Window.width,
'y': ( self.y + touch.dy ) / Window.height }
return True
class EditBoreholeScreen( Screen ):
def __init__( self, **kwargs ):
super( EditBoreholeScreen, self ).__init__( **kwargs )
def open_popup( self ):
popup = DragPopup( title = "Testing",
content = Label( text = "Drag me" ),
size_hint = ( None, None ),
pos = ( 200, 200 ),
size = ( 600, 400 ) )
popup.open( )
class TestApp( App ):
def build( self ):
sm = ScreenManager( )
self.sm = sm
sm.add_widget( EditBoreholeScreen( name = "edit" ) )
return sm
if __name__ == '__main__':
root = TestApp( )
root.run( )
#:kivy 1.10
<DragLabel>:
drag_rectangle: self.x, self.y, self.width, self.height
drag_timeout: 1000000
drag_distance: 0
<DragPopup>:
drag_rectangle: self.x, self.y+self._container.height, self.width, self.height - self._container.height
drag_timeout: 10000000
drag_distance: 0
<EditBoreholeScreen>:
FloatLayout:
canvas.before:
Color:
rgba: ( 1, 0, 0, 1 )
Rectangle:
pos: self.pos
size: self.size
Button:
size_hint: None, None
text: "Open Popup"
pos: 100, 100
size: 200, 100
on_release: root.open_popup( )
DragLabel:
size_hint: None, None
text: "Testing"
pos: 400, 150
size: 100, 50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment