Kivy: Swiping (Carousel & ScreenManager) -
i have 2 screens in screenmanager both contains number of buttons in scrollview. idea 1 steps forward (right) clicking button. , step (left) swiping back. trying add carousel implement 1 swipe on second page. have tried:
self.root = screenmanager(id = 'screen_manager') main_screen = screen(name = 'main_screen') scroller = scroller() button_text = ['teach', 'move', 'b3', 'b4', 'b5', 'b6', 'b7', 'b8'] text in button_text: scroller.view.add_widget(field(name=text, direction='left', current='teach')) main_screen.add_widget(scroller) self.root.add_widget(main_screen) carousel = carousel(direction='left', id='carousel') teach = screen(name = 'teach') scroller2 = scroller() button_text = ['vocab', 'drills'] text in button_text: scroller2.view.add_widget(field(name=text, direction='right', current='main_screen')) carousel.add_widget(scroller2) teach.add_widget(carousel) self.root.add_widget(teach)
but since have added second screen, it's not possible swipe in either direction. carousel's load_slide()
method takes slide argument. assuming slide mean carousel. given going have lot pages, need carousel loaded dynamically, using add_widget()
, remove_widget()
. appreciate pointers.
working example of code have far: http://dpaste.com/33464r2
you can using screenmanager , gestures. (../kivy/examples/gestures/)
see here kivy-github-gestures
i have explained in code in comments.
first need create new python file named gesture_box.py.
gesture_strings copy here
from kivy.gesture import gesturedatabase kivy.uix.boxlayout import boxlayout kivy.gesture import gesture [paste gesture_strings here] #this database can compare gestures user makes stored gestures #and tell if user input matches of them. gestures = gesturedatabase() name, gesture_string in gesture_strings.items(): gesture = gestures.str_to_gesture(gesture_string) gesture.name = name gestures.add_gesture(gesture) class gesturebox(boxlayout): def __init__(self, **kwargs): name in gesture_strings: self.register_event_type('on_{}'.format(name)) super(gesturebox, self).__init__(**kwargs) def on_left_to_right_line(self): pass #to recognize gesture, you’ll need start recording each individual event in #touch_down handler, add data points each call touch_move , , #gesture calculations when data points have been received in touch_up handler. def on_touch_down(self, touch): #create user defined variable , add touch coordinates touch.ud['gesture_path'] = [(touch.x, touch.y)] super(gesturebox, self).on_touch_down(touch) def on_touch_move(self, touch): touch.ud['gesture_path'].append((touch.x, touch.y)) super(gesturebox, self).on_touch_move(touch) def on_touch_up(self, touch): if 'gesture_path' in touch.ud: #create gesture object gesture = gesture() #add movement coordinates gesture.add_stroke(touch.ud['gesture_path']) #normalize thwu willtolerate size variations gesture.normalize() #minscore attained match true match = gestures.find(gesture, minscore=0.3) if match: print("{} happened".format(match[1].name)) self.dispatch('on_{}'.format(match[1].name)) super(gesturebox, self).on_touch_up(touch)
now create main.py file.
import gesture_box gesture kivy.app import app kivy.uix.boxlayout import boxlayout class runner(gesture.gesturebox): pass class mainapp(app): def build(self): return runner() if __name__ == '__main__': app = mainapp() app.run()
and main.kv file
<runner>: #handling gesture event. on_left_to_right_line: manager.current = 'main_screen'; manager.transition.direction = 'right' screenmanager: id: manager screen: name: "main_screen" boxlayout: orientation: 'vertical' label: button: text: "child 1" on_release: manager.current = "child1" manager.transition.direction = 'left' label: button: text: "child 2" on_release: manager.current = "child1" manager.transition.direction = 'left' label: screen: name: "child1" label: text: "swipe left right go main screen (child 1)" screen: name: "child2" label: text: "swipe left right go main screen (child 1)"
edit: many people have asked me how these gesture string generated.
kivy guys provide in examples.
see here https://github.com/kivy/kivy/blob/master/examples/gestures/gesture_board.py run file.
python gesture_board.py
it should open blank window.
make gesture on using mouse or touch.
when on_touch_up event triggered, output gesture string in terminal.
for example output right_to_left_line be. this
('gesture representation:', 'enq1wmtshecqvm+piis36t3dp4cujuadhfhsaceznmblw3/vnorzxycwmypfqqu5odlvldxvpbp6/pr494/n/fz1//1lo3yepnc0xn3telj59ht71/btsbp8ig8dxm8+ve5fnr9ux/gndvffdj5cvstyk7rhf6nuwfo758en/fhyhr9rfx77fwqno+4rjch8wcmsw/vvtcfhzwuspjxzfqzn3/frha5pe6wica0nh00agv3z9unflbfx4f6i/v0krrkspze7pnyfdkbnzyu0mykjzo3mxlzi15ruy9lu4zwksifi9biolvl14rxshi3xhqulqyxhlzls+iuk00zzyamfmqoyyaen5hufsrtlhy0mptzq6mjsxpev00vp/7+ypom6wkq0degrpxqgtw250pom6got1biqrwth0ddflk4t9cxerksnptrg3vvcm4tupvr2gmouy+jpo57zyfzi1acoddjbsv0co9frkohcpdqvoaev2n6nuqwddmyntwiw4uwmcvto4olodlpn0qyyy7j4gmpnhhker7w0vkngxzsjojdz9etwibf3fz1o6amdeyrkqjdegmae1ob9dzqptegbtw0ujmiv8umqta6mehxejvvauhlbpdyqq1ndszpizuesy+rpqrwtulqphuhfegr4el4cxjmbsyiaet5ili+ke5rqfblhuckkbega3bqdxolnx7rl8ttv9sqecfwca3hg59ip6krnmgsdylhskxwrp6fua8q5qfondc/svcq8lj6oelku76mhuhhagrqxjja73dnsb6vkrwrbnrjwysm226j6pkdx8psxu4nmeaix8jptwsmkruafzmm2bgmkpxgyfiexozmrekaukcpjnxghaaptw4hwxz4wkzagrxjhw1qmn6/ncbtauzw0nmoqcvyczdb9wa0jhpydjxq02nlnhfanyeybeazmremsdhze0yzyobhnrcmsaepahi4l/sy6up9hcvw+mxqkegyt4ysitz+ll8/fub1h7uj72koyyhkhgc/5x16226fjlb3eegcvzbi6xistgxqug6e87heldrej2gxthtu5ryt1vgyklttkm4uqislogaodtbmcdztnxe+uy64bhawyosvr8rlc5oazcazmam3objkzfnu085+ryj2qswkm96dqbzphsl1fjmamintebydmzoizkabozeywui9ewtz74zgqucykzvoomwpylzj+ezeimxup/h1cvsdrpncdwc5ldoqoi5p54x4/rznx5zdozo53tzmaqensmeczd3ofr2fdlnliyeztshsd0ezfpqwdfioau+zvrjczwqctlzu4i6hlsgznyxrqihcz0hkfagtb1d5gzpfup2bkivxmveksnr564ibm7xtaokc06yxtljmiwsopc1soozrhsxyyvevgn2t+udhp0cxzfn4kknmkzvtsvkpz1phmtdtmaonom3yv55seqlqpkd1rke9qft5imhmd7ivpvu7jodj/0uv0xkchlfrewa4r6b3kl8cezotoomumbsw01aybxdqh8weohnb+0eyt8860w90kgsuumqwfqnojmi1rvf8m6jfh+we0bb8j2g+g6fw5oqhfpzgfto/3d/vx/6tw3nntbzyctim4/zze7r+sesn4ao0ran4/f9u+3d592ezxjd8srnw65f/yvtzfff/stetwrru8hucvaft0ps484+v98x/wyond') ('cross:', -1.2199187170964643) ('check:', -2.052277818300959) ('circle:', 0.4973932910874005) ('square:', 0.2907537534396739)
that's it, have string :d
Comments
Post a Comment