【Kivy】Clock を利用する

定期的に実行する

  • main.py
from kivy.app import App
from kivy.clock import Clock

class MainApp(App):
    def on_start(self):
        Clock.schedule_interval(self.on_callback, 1.)
    def on_callback(self, delta: float):
        print('on_callback')

if __name__ == '__main__':
    MainApp().run()

定期的に実行した後、停止する

  • main.py
from kivy.app import App
from kivy.clock import Clock
from kivy.clock import ClockEvent

class MainApp(App):
    def on_start(self):
        self.__count: int = 0
        self.__clock_event: ClockEvent = Clock.schedule_interval(self.on_callback, 1.)
    def on_callback(self, delta: float):
        print('on_callback : self.__count = {}'.format(self.__count))
        self.__count += 1
        if self.__count > 5:
            self.__clock_event.cancel()

if __name__ == '__main__':
    MainApp().run()

1 度だけ実行する

  • main.py
from kivy.app import App
from kivy.clock import Clock

class MainApp(App):
    def on_start(self):
        Clock.schedule_once(self.on_callback, 1.)
    def on_callback(self, delta: float):
        print('on_callback')

if __name__ == '__main__':
    MainApp().run()

【Kivy】StackLayout を利用して敷き詰めるように並べる

  • main.py
from kivy.app import App

class MainApp(App):
    pass

if __name__ == '__main__':
    MainApp().run()
  • main.kv
#:kivy 1.0
StackLayout:
    Button:
        text: 'button 1'
        size_hint: None, None
        size: 400, 80
    Button:
        text: 'button 2'
        size_hint: None, None
        size: 500, 160
    Button:
        text: 'button 3'
        size_hint: None, None
        size: 300, 200
    Button:
        text: 'button 4'
        size_hint: None, None
        size: 120, 400
    Button:
        text: 'button 5'
        size_hint: None, None
        size: 800, 80
    Button:
        text: 'button 6'
        size_hint: None, None
        size: 600, 200

【Kivy】ScatterLayout を利用してユーザが自由配置できるようにする

  • main.py
from kivy.app import App

class MainApp(App):
    pass

if __name__ == '__main__':
    MainApp().run()
  • main.kv
#:kivy 1.0
ScatterLayout:
    Button:
        text: 'button 1'
        size_hint: None, None
        size: 800, 600

【Kivy】RelativeLayout を利用し、画面サイズに応じて配置する

  • main.py
from kivy.app import App

class MainApp(App):
    pass

if __name__ == '__main__':
    MainApp().run()
  • main.kv
#:kivy 1.0
RelativeLayout:
    Button:
        text: 'button 1'
        size_hint: None, None
        size: 400, 1200
        pos_hint: {'x': .4, 'y': .1}
    Button:
        text: 'button 2'
        size_hint: None, None
        size: 200, 300
        pos_hint: {'x': .1, 'y': .5}
    Button:
        text: 'button 3'
        size_hint: None, None
        size: 200, 200
        pos_hint: {'x': .2, 'y': .3}
    Button:
        text: 'button 4'
        size_hint: None, None
        size: 400, 80
        pos_hint: {'x': .5, 'y': .2}

【Kivy】PageLayout でページ送りを実装する

  • main.py
from kivy.app import App

class MainApp(App):
    pass

if __name__ == '__main__':
    MainApp().run()
  • main.kv
#:kivy 1.0
PageLayout:
    Button:
        text: 'page 1'
    Button:
        text: 'page 2'
    Button:
        text: 'page 3'

【Kivy】AnchorLayout を利用し、アンカーポイントを基準に配置する

  • main.py
from kivy.app import App

class MainApp(App):
    pass

if __name__ == '__main__':
    MainApp().run()
  • main.kv
#:kivy 1.0
AnchorLayout:
    anchor_x: 'right'
    anchor_y: 'top'
    Button:
        text: 'button 1'
        size_hint: None, None
        size: 400, 100