Last active
March 20, 2018 16:14
-
-
Save dolang/ebbf7d263bd9ab74a5a1bdf5d90bc05e to your computer and use it in GitHub Desktop.
Simple carousel example with Kivy using ScreenManager, doing automatic transitions.
This file contains hidden or 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
| """ | |
| Simple `ScreenManager` carousel with screen widgets and optionally, a | |
| shader transition. | |
| This `ScreenManager` changes its `Screen` widgets every 2 seconds (note | |
| the `Clock.schedule_interval()` in `CarouselTest1.__init__()`. | |
| Additional transitions can be triggered with the toggle button. | |
| """ | |
| from __future__ import print_function, unicode_literals | |
| # import dbg_cfg | |
| import kivy | |
| kivy.require('1.10.0') | |
| from kivy.clock import Clock | |
| from kivy.uix.button import Button | |
| from kivy.uix.floatlayout import FloatLayout | |
| from kivy.app import App | |
| from kivy.lang import Builder | |
| from kivy.uix.screenmanager import ScreenManager, FadeTransition, WipeTransition | |
| from kivy.logger import Logger | |
| KV = ''' | |
| #:kivy 1.10.0 | |
| CarouselTest1: | |
| Screen: | |
| name: 'screen1' | |
| canvas: | |
| Color: | |
| rgba: 0.5, 1, 1, 0.5 | |
| Rectangle: | |
| pos: self.pos | |
| size: self.size | |
| Screen: | |
| name: 'screen2' | |
| canvas: | |
| Color: | |
| rgba: 1, 0.5, 0.5, 0.5 | |
| Rectangle: | |
| pos: self.pos | |
| size: self.size | |
| ''' | |
| toggle_counter = 0 | |
| class CarouselTest1(ScreenManager): | |
| def __init__(self, **kwargs): | |
| super(CarouselTest1, self).__init__(**kwargs) | |
| Clock.schedule_interval(self._toggle, 2) | |
| ########################## | |
| # change transition here # | |
| ########################## | |
| # self.transition = WipeTransition() | |
| def _toggle(self, *_): | |
| global toggle_counter | |
| toggle_counter += 1 | |
| if toggle_counter % 10 == 0: | |
| Logger.debug("Did {} transitions so far".format(toggle_counter)) | |
| self.current = self.next() | |
| class CarouselTest1App(App): | |
| def build(self): | |
| layout = FloatLayout() | |
| car_test = Builder.load_string(KV) | |
| layout.add_widget(car_test) | |
| layout.add_widget(Button(text='toggle', size_hint=(0.1,0.1), | |
| on_press=lambda *_:self.root.children[-1]._toggle())) | |
| return layout | |
| if __name__ == '__main__': | |
| CarouselTest1App().run() |
This file contains hidden or 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
| """ | |
| Debug configuration for easy import (instead of env vars/config file). | |
| Uncomment ``import dbg_cfg`` in the main file, to see debug output. | |
| """ | |
| # import os | |
| # os.environ['KIVY_NO_CONFIG'] = '1' # if active, no other config file is loaded | |
| from kivy import Config | |
| Config.set('kivy', 'log_level', 'debug') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment