## 2019年2月17日日曜日

### Python - Kivy - キャンバス(回転し続ける長方形、Clock、schedule_intervalメソッド、コールバック関数)

Kivyプログラミング ―Pythonで作るマルチタッチアプリ― (実践Pythonライブラリー) (原口 和也(著)、久保 幹雄(監修)、朝倉書店)の5(キャンバス)、5.4(演習問題)、演習5.3の解答を求めてみる。

コード

Python 3

```#!/usr/bin/env python3
from kivy.app import App, Widget
from kivy.clock import Clock
from kivy.graphics import Color, Rotate, Rectangle
# 日本語用
from kivy.core.text import LabelBase, DEFAULT_FONT
LabelBase.register(DEFAULT_FONT, 'NotoSansCJKjp-Regular.otf')

class MyWidget(Widget):
evn = None

def __init__(self, *args, **kwargs):
Widget.__init__(self, *args, **kwargs)
self.rect_pos_x = 600
self.rect_pos_y = 500
self.rect_pos = self.rect_pos_x, self.rect_pos_y
self.rect_width = 400
self.rect_height = 100
self.rect_size = self.rect_width, self.rect_height
self.rotate_origin_x = self.rect_pos_x + self.rect_width / 2
self.rotate_origin_y = self.rect_pos_y + self.rect_height / 2
self.rotate_origin = self.rotate_origin_x, self.rotate_origin_y
self.angle = 0
self.evn = Clock.schedule_interval(self.update, 0.01)

def update(self, dt):
self.canvas.clear()
with self.canvas:
Rotate(origin=self.rotate_origin, angle=self.angle)
Color(rgb=(0, 255, 0))
Rectangle(pos=self.rect_pos, size=self.rect_size)
self.angle += dt * 100

class Sample3App(App):
def build(self):
return MyWidget()

Sample3App().run()
```

```\$ python3 sample3.py
[INFO   ] [Logger      ] Record log in /Users/...
[INFO   ] [Kivy        ] v1.10.1
[INFO   ] [Python      ] v3.7.2 (default, Dec 30 2018, 08:55:50)
[Clang 10.0.0 (clang-1000.11.45.5)]
[INFO   ] [Factory     ] 194 symbols loaded
[INFO   ] [Image       ] Providers: img_tex, img_imageio, img_dds, img_sdl2, img_pil, img_gif (img_ffpyplayer ignored)
[INFO   ] [Text        ] Provider: sdl2
[INFO   ] [Window      ] Provider: sdl2
[INFO   ] [GL          ] Using the "OpenGL ES 2" graphics system
[INFO   ] [GL          ] Backend used <gl>
[INFO   ] [GL          ] OpenGL version <b'2.1 INTEL-12.4.7'>
[INFO   ] [GL          ] OpenGL vendor <b'Intel Inc.'>
[INFO   ] [GL          ] OpenGL renderer <b'Intel(R) Iris(TM) Pro Graphics 6200'>
[INFO   ] [GL          ] OpenGL parsed version: 2, 1
[INFO   ] [GL          ] Shading version <b'1.20'>
[INFO   ] [GL          ] Texture max size <16384>
[INFO   ] [GL          ] Texture max units <16>
[INFO   ] [Window      ] auto add sdl2 input provider
[INFO   ] [Window      ] virtual keyboard not allowed, single mode, not docked
[INFO   ] [Base        ] Start application main loop
[INFO   ] [Base        ] Leaving application in progress...
\$
```