2019年1月3日木曜日

開発環境

Kivyプログラミング ―Pythonで作るマルチタッチアプリ― (実践Pythonライブラリー) (原口 和也(著)、久保 幹雄(監修)、朝倉書店)の3(イベントとプロパティ)、3.4(演習問題)、演習3.2の解答を求めてみる。

コード

Python 3

#!/usr/bin/env python3
from kivy.app import App
from kivy.clock import Clock
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout

from kivy.core.text import LabelBase, DEFAULT_FONT

import random

LabelBase.register(DEFAULT_FONT, 'NotoSansCJKjp-Regular.otf')


class MyLabel(Label):
    def __init__(self, *args, **kwargs):
        Label.__init__(self, *args, **kwargs)
        self.text = str(random.randrange(10))
        self.event = Clock.schedule_interval(self.callback, 0.2)

    def callback(self, dt):
        self.text = str((int(self.text) + 1) % 10)


class MyButton(Button):
    def __init__(self, *args, **kwargs):
        Button.__init__(self, *args, **kwargs)
        self.text = '停止'
        self.index = 2

    def on_press(self):
        labels = self.parent.children[1].children
        if self.index >= 0:
            labels[self.index].event.cancel()
            self.index -= 1


class SampleApp(App):
    def build(self):
        box_layout1 = BoxLayout(orientation='vertical')
        box_layout2 = BoxLayout(orientation='horizontal')
        labels = [MyLabel(font_size=100, size_hint_y=0.8) for _ in range(3)]
        for label in labels:
            box_layout2.add_widget(label)
        box_layout1.add_widget(box_layout2)
        box_layout1.add_widget(MyButton(size_hint_y=0.2))
        return box_layout1


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

入出力結果(Terminal, cmd(コマンドプロンプト), Jupyter(IPython))

$ ./sample2.py
[INFO   ] [Logger      ] Record log in /~/.kivy/logs/kivy_19-01-04_31.txt
[INFO   ] [Kivy        ] v1.10.1
[INFO   ] [Python      ] v3.7.1 (default, Oct 21 2018, 09:01:26) 
[Clang 10.0.0 (clang-1000.11.45.2)]
[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   ] [GL          ] NPOT texture support is available
[INFO   ] [Base        ] Leaving application in progress...
$

0 コメント:

コメントを投稿