Skip to content

Instantly share code, notes, and snippets.

@gottadiveintopython
Created October 23, 2017 07:06
Show Gist options
  • Save gottadiveintopython/2e0a16a572d58c8fb02454be006bf02d to your computer and use it in GitHub Desktop.
Save gottadiveintopython/2e0a16a572d58c8fb02454be006bf02d to your computer and use it in GitHub Desktop.
kivy.garden.magnet test
import random
from kivy.base import runTouchApp
from kivy.lang import Builder
from kivy.factory import Factory
from kivy.properties import ListProperty
import kivy.utils
from kivy.garden.magnet import Magnet
Builder.load_string(r'''
<Card>:
font_size: 50
canvas.before:
Color:
rgba: .3, .3, .3, 1
RoundedRectangle
size: self.size
pos: self.pos
Color:
rgba: self.background_color
RoundedRectangle
size: self.width - 6, self.height - 6
pos: self.x + 3, self.y + 3
''')
class Card(Factory.Label):
background_color = ListProperty()
def create_random_card():
return Card(
text=random.choice(r'A 2 3 4 5 6 7 8 9 10 J Q K'.split()),
color=(1, 1, 1, .5, ),
background_color=kivy.utils.get_random_color())
root = Factory.GridLayout(cols=6, spacing=10, padding=[10])
def add_random_card(widget, *args):
magnet = Magnet(duration=0.5)
magnet.add_widget(create_random_card())
widget.add_widget(magnet)
root.bind(on_touch_down=add_random_card)
runTouchApp(root)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment