Skip to content

Instantly share code, notes, and snippets.

@gottadiveintopython
Last active September 30, 2018 02:59
Show Gist options
  • Save gottadiveintopython/a40198678a2ada7882edfb410bdb33e8 to your computer and use it in GitHub Desktop.
Save gottadiveintopython/a40198678a2ada7882edfb410bdb33e8 to your computer and use it in GitHub Desktop.
slide in menu. old code. (Kivy)
# -*- coding: utf-8 -*-
import kivy
kivy.require('1.9.1')
from kivy.base import runTouchApp
from kivy.lang import Builder
from kivy.factory import Factory
from kivy.animation import Animation
Builder.load_string(r'''
<MenuItem@ButtonBehavior+Label>:
font_size: self.height
canvas.before:
Color:
rgba: [.2, .2, .2, 1] if self.state == 'down' else [0, 0, 0, 0]
Rectangle:
pos: self.pos
size: self.size
Color:
rgba: [1, 1, 1, 1]
Line:
rectangle: [self.x, self.y, self.width, self.height]
on_press: print("'{}' button was pressed.".format(self.text))
<Menu@BoxLayout>:
layout: id_layout
orientation: 'vertical'
padding: [5]
spacing: 20
Label:
size_hint_y: 1
text: 'Menu'
font_size: self.height
ScrollView:
size_hint_y: 5
do_scroll_x: False
GridLayout:
id: id_layout
size_hint: 1, None
height: self.minimum_height
cols: 1
row_default_height: self.parent.height / 7
row_force_default: True
spacing: 10
<SlideinMenu>:
canvas:
Clear:
Color:
rgba: 0, 0, 0, 0.7
Rectangle:
pos: 0, 0
size: self.size if self.parent is None else self.parent.size
''')
class SlideinModalView(Factory.ModalView):
def open(self, *args, **kwargs):
size_hint_x = self.size_hint_x
self.pos_hint = {'x': -size_hint_x}
animation = Animation(
pos_hint={'x': (1 - size_hint_x) / 2},
duration=0.3,
transition='in_cubic'
)
super(SlideinModalView, self).open(*args, **kwargs)
animation.start(self)
def dismiss(self, *args, **kwargs):
size_hint_x = self.size_hint_x
animation = Animation(
pos_hint={'x': -size_hint_x},
duration=0.3,
transition='in_out_bounce'
)
super(SlideinModalView, self).dismiss(*args, **kwargs)
animation.start(self)
class SlideinMenu(SlideinModalView):
def __init__(self, **kwargs):
super(SlideinMenu, self).__init__(**kwargs)
self.menu = menu = Factory.Menu()
self.layout = menu.layout
self.add_widget(menu)
def _test():
import textwrap
slideinmenu = SlideinMenu(size_hint=(0.8, 0.8,))
layout = slideinmenu.layout
for text in 'Python Ruby C++ Smalltalk Javascript C Swift'.split():
layout.add_widget(Factory.MenuItem(text=text))
root = Builder.load_string(textwrap.dedent('''
FloatLayout:
Button:
id: id_button
size_hint: None, None
size: 300,100
text: 'open menu'
'''))
root.ids.id_button.bind(on_press=slideinmenu.open)
runTouchApp(root)
if __name__ == '__main__':
_test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment