2018年12月25日火曜日

開発環境

Kivyプログラミング ―Pythonで作るマルチタッチアプリ― (実践Pythonライブラリー) (原口 和也(著)、久保 幹雄(監修)、朝倉書店)の2(ウィジェット)、2.5(演習問題)、演習2.5を取り組んでみる。

コード(Emacs)

Python 3

#!/usr/bin/env python3
from kivy.app import App
from kivy.uix.textinput import TextInput
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

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


class LeftButton(Button):
    def __init__(self, *args, **kwargs):
        Button.__init__(self, *args, **kwargs)

    def on_press(self):
        print('on_press(LeftButton)')
        box_layout = self.parent.parent.children[0]
        box_layout.orientation = 'horizontal'
        for widget in box_layout.children[:]:
            box_layout.remove_widget(widget)
        label_a = Label(text='A', size_hint_x=1)
        button_b = Button(text='B', size_hint_x=1)
        label_c = Label(text='C', size_hint_x=1)
        for widget in [label_a, button_b, label_c]:
            box_layout.add_widget(widget)


class RightButton(Button):
    def __init__(self, *args, **kwargs):
        Button.__init__(self, *args, **kwargs)

    def on_press(self):
        print('on_press(RightButton)')
        box_layout = self.parent.parent.children[0]
        box_layout.orientation = 'vertical'
        for widget in box_layout.children[:]:
            box_layout.remove_widget(widget)
        label_d = Label(text='D', size_hint_y=0.2)
        button_e = Button(text='E', size_hint_y=0.2)
        label_f = Label(text='F', size_hint_y=0.2)
        button_g = Button(text='G', size_hint_y=0.2)
        for widget in [label_d, button_e, label_f, button_g]:
            box_layout.add_widget(widget)


class SwitchScreen(App):
    def build(self):
        box_layout = BoxLayout(orientation='vertical')
        box_layout1 = BoxLayout(orientation='horizontal', size_hint_y=0.2)
        box_layout2 = BoxLayout(orientation='horizontal', size_hint_y=0.8)
        left_button = LeftButton(text='Left')
        right_button = RightButton(text='Right')
        for widget in [box_layout1, box_layout2]:
            box_layout.add_widget(widget)
        for widget in [left_button, right_button]:
            box_layout1.add_widget(widget)
        left_button.on_press()
        return box_layout


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

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

$ ./sample5.py
[INFO   ] [Logger      ] Record log in /~/.kivy/logs/kivy_18-12-25_55.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
on_press(LeftButton)
[INFO   ] [Base        ] Start application main loop
[INFO   ] [GL          ] NPOT texture support is available
on_press(LeftButton)
on_press(RightButton)
on_press(RightButton)
on_press(LeftButton)
on_press(RightButton)
on_press(LeftButton)
[INFO   ] [Base        ] Leaving application in progress...
$

0 コメント:

コメントを投稿