開発環境
- macOS Mojave - Apple (OS)
- Emacs (Text Editor)
- Windows 10 Pro (OS)
- Visual Studio Code (Text Editor)
- Python 3.7 (プログラミング言語)
- Kivy (パッケージ)
Kivyプログラミング ―Pythonで作るマルチタッチアプリ― (実践Pythonライブラリー) (原口 和也(著)、久保 幹雄(監修)、朝倉書店)の2(ウィジェット)、2.5(演習問題)、演習2.4を取り組んでみる。
コード(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 MyTextInput(TextInput):
def __init__(self, *args, **kwargs):
TextInput.__init__(self, *args, **kwargs)
self.multiline = False
def on_text_validate(self):
print('on_text_validate(TextInput)')
label = self.parent.children[1]
label.text = self.text
class MyButton(Button):
def __init__(self, *args, **kwargs):
Button.__init__(self, *args, **kwargs)
self.current_color_index = 0
def on_press(self):
print('on_press(Button)')
colors = [('白', (1, 1, 1, 1)),
('青', (0, 0, 1, 1)),
('赤', (1, 0, 0, 1))]
label = self.parent.children[1]
self.current_color_index = (self.current_color_index + 1) % 3
_, color_rgba = colors[self.current_color_index]
color_name, _ = colors[(self.current_color_index + 1) % 3]
self.text = color_name
label.color = color_rgba
class SampleApp(App):
def build(self):
boxlayout = BoxLayout(orientation='vertical')
text_input = MyTextInput(text='I love python+kivy!')
label = Label(text='I love python+kivy!')
button = MyButton(text='青')
for widget in [text_input, label, button]:
boxlayout.add_widget(widget)
return boxlayout
if __name__ == '__main__':
SampleApp().run()
入出力結果(Terminal, cmd(コマンドプロンプト), Jupyter(IPython))
$ ./sample4.py [INFO ] [Logger ] Record log in /~/.kivy/logs/kivy_18-12-24_19.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 ] [GL ] NPOT texture support is available [INFO ] [Base ] Start application main loop on_press(Button) on_press(Button) on_press(Button) on_press(Button) on_text_validate(TextInput) on_press(Button) on_text_validate(TextInput) [INFO ] [Base ] Leaving application in progress... $
TextInputクラスで日本語入力を直接出来るようにはまだ出来てない(他のアプリで入力してコピーアンドペーストなら可能)のでいずれ出来るようにしてみることに。その時までに、日本語ユーザーが増えて、Kivyが対応してくれることをちょっと期待。
TextInputに他のアプリで入力してコピーアンドペーストでの日本語入力なら、ラベルにもちゃんと反映されることを確認。









0 コメント:
コメントを投稿