Pythonista3でpythonプログラミング
pythonプログラミングに利用している
iOSアプリのPythonista3
mecobalamin.hatenablog.com
mecobalamin.hatenablog.com
割と色々できて便利
まずはpythonistaについて
iOS上で動作する革命的ものづくり環境「Pythonista 3」の魅力をとくと語る
iOS上で動作する革命的ものづくり環境「Pythonista 3」の魅力をとくと語る
iPad ProでPythonプログラム
iPad ProでPythonプログラム - Qiita
Deep Learningの勉強にも使っていたり
「ゼロから作るDeep Learning」をiPhoneのPythonistaだけで学ぶ(2)
「ゼロから作るDeep Learning」をiPhoneのPythonistaだけで学ぶ(2) - blog.tmp.tokyo
iPadで「ゼロから作るDeep Learning」を勉強するために必要なこと
poipoides.hatenablog.com
自分でも試してみたけどコードに変更が必要で
上記のリンク先の方が変更したコードを載せてくれている
3章か4章ぐらいまではpythonistaで動くことを確認した
その先は結局PCのpythonで。
今までやったのは
コンソールに結果を表示する
プログラミング
希望する機能を実装できていたけど
せっかくiPadを使っているのだから
タッチ操作できるプログラミングをしてみたい
pythonistaにはいくつもサンプルコードが入っていて
そのいくつかはタッチ操作のゲームだったりする
実際ゲームを作っている人達もいる
Pythonistaで作るポーカー作成講座(全5回)
Pythonistaで作るポーカー作成講座(全5回) | みやびのどっとぴーわい
Pythonista+sceneでトランプをランダムに表示する
Pythonista+sceneでトランプをランダムに表示する - Qiita
公式でXcodeに変換するコードを公開しているので
その気になればiOSアプリも作れそう
python2に対応
github.com
で、試しに作ってみた
作ったのは例えば何かカードゲームをして
得点を記録するプログラム
一度コンソールに表示するバージョンは書いていて
こんな感じ
それをこのように表示したい
プレイヤーは3人で
ポイントを入力してsubmitを押すと
totalに加算される
そしてroundが一つ進む
コードはpythonプログラムのui.pyと
ボタン等を配置したui.pyuiの2つで一セット
実際のコードはこの記事の最後に載せるとして
ui.pyの説明を書く
import ui import console
pythonistaにはsceneというライブラリもあるが
今回使用したのはui
consoleもpythonistaのライブラリで
コンソールを操作するのに使う
v = ui.load_view() v['num1'].keyboard_type = ui.KEYBOARD_NUMBER_PAD v['num2'].keyboard_type = ui.KEYBOARD_NUMBER_PAD v['num3'].keyboard_type = ui.KEYBOARD_NUMBER_PAD v.present('fullscreen')
ui.load_view()でui.pyuiをロードしている
v['num1'].keyboard_typeは
num1に入力するときのキーボードの種類を設定する
num1は得点の入力なので数値のみ
なので最初から数字入力になっている
キーボードが表示されるようにしている
present('fullsucreen')で
フルスクリーン表示でコードを実行する
よくわかっていないんだけど
このときプログラムは×印をタップして停止させるまで
入力待ちになっているっぽい
関数を実行するにはui.pyに書かれた関数を
ui.pyuiのボタンに紐付けしておく
ボタンをタップすると
紐付けされた関数on_buttonが実行される
def on_button(sender):
引数のsenderには画面上の情報が入っているようで
superview[]とボタンやtextfieldの名前を使って値を取り出す
text_label1 = sender.superview['label1'] Num_1 = sender.superview['num1']
label1やnum1はtextfieldの名前であり
ui.pyuiで編集できる
print文はconsol画面に表示されるため
プログラムを停止するまで表示を見られない
書いただけなので読みやすく修正したいけど
とりあえずここまででやりたいことは実装できた
ui.pyuiも見やすいデザインにしたい
まだ読んでいないけどpytonista3を使った
プログラミング教本らしいのでメモ
PythonではじめるiOSプログラミング 〜iOS+Pythonで数値処理からGUI、ゲーム、iOS機能拡張まで〜
実際のコードは以下の通り
ui.py
import ui import console def on_button(sender): text_label1 = sender.superview['label1'] Num_1 = sender.superview['num1'] Re_1 = sum_score(text_label1.text, Num_1.text) text_label1.text = str(Re_1) Num_1.text = '' text_label2 = sender.superview['label2'] Num_2 = sender.superview['num2'] Re_2 = sum_score(text_label2.text, Num_2.text) text_label2.text = str(Re_2) Num_2.text = '' text_label3 = sender.superview['label3'] Num_3 = sender.superview['num3'] Re_3 = sum_score(text_label3.text, Num_3.text) text_label3.text = str(Re_3) Num_3.text = '' num_round = sender.superview['num_round'] num_round.text = str(int(num_round.text) + 1) #print(Num_3.text_color) print('round' + str(int(num_round.text) - 1), Re_1, Re_2, Re_3) def sum_score(m, n): r = 0 if m == '': r = int(n) elif n == '': r = int(m) else: r = int(m) + int(n) return r if __name__ == '__main__': console.clear() console.set_font('Menlo',20) v = ui.load_view() v['num1'].keyboard_type = ui.KEYBOARD_NUMBER_PAD v['num2'].keyboard_type = ui.KEYBOARD_NUMBER_PAD v['num3'].keyboard_type = ui.KEYBOARD_NUMBER_PAD v.present('fullscreen')
ボタンや数字の入力場所の配置はこんな感じ
赤でマークしたactionにon_button関数を登録した
submitのボタンを押すと
on_button関数が実行される
ui.pyuiはテキストファイルなのでエディタで中身が読める
よくわからないけどファイルの形式はJSON?っぽく見える
ui.pyui
[ { "nodes" : [ { "nodes" : [ ], "frame" : "{{119, 54}, {77, 43}}", "class" : "TextField", "attributes" : { "uuid" : "EB343BBB-5CFB-4AE7-8BCC-41EE1ACDE42E", "font_size" : 17, "frame" : "{{60, 224}, {200, 32}}", "custom_attributes" : "", "action" : "", "alignment" : "right", "autocorrection_type" : "default", "text" : "", "font_name" : "<System>", "spellchecking_type" : "default", "class" : "TextField", "name" : "num1", "flex" : "WHLRTB" }, "selected" : false }, { "nodes" : [ ], "frame" : "{{16, 54}, {95, 43}}", "class" : "TextField", "attributes" : { "uuid" : "EB343BBB-5CFB-4AE7-8BCC-41EE1ACDE42E", "font_size" : 17, "frame" : "{{60, 224}, {200, 32}}", "action" : "", "alignment" : "left", "autocorrection_type" : "default", "text" : "", "font_name" : "<System>", "spellchecking_type" : "default", "class" : "TextField", "name" : "usr1", "flex" : "WHLRTB" }, "selected" : false }, { "nodes" : [ ], "frame" : "{{16, 105}, {95, 43}}", "class" : "TextField", "attributes" : { "flex" : "WHLRTB", "uuid" : "EB343BBB-5CFB-4AE7-8BCC-41EE1ACDE42E", "frame" : "{{60, 224}, {200, 32}}", "action" : "", "alignment" : "left", "autocorrection_type" : "default", "text" : "", "font_name" : "<System>", "spellchecking_type" : "default", "class" : "TextField", "name" : "usr2", "font_size" : 17 }, "selected" : false }, { "nodes" : [ ], "frame" : "{{16, 156}, {95, 43}}", "class" : "TextField", "attributes" : { "flex" : "WHLRTB", "uuid" : "EB343BBB-5CFB-4AE7-8BCC-41EE1ACDE42E", "frame" : "{{60, 224}, {200, 32}}", "action" : "", "alignment" : "left", "autocorrection_type" : "default", "text" : "", "font_name" : "<System>", "spellchecking_type" : "default", "class" : "TextField", "name" : "usr3", "font_size" : 17 }, "selected" : false }, { "nodes" : [ ], "frame" : "{{119, 105}, {77, 43}}", "class" : "TextField", "attributes" : { "uuid" : "EB343BBB-5CFB-4AE7-8BCC-41EE1ACDE42E", "font_size" : 17, "frame" : "{{60, 224}, {200, 32}}", "action" : "", "alignment" : "right", "autocorrection_type" : "default", "text" : "", "font_name" : "<System>", "spellchecking_type" : "default", "class" : "TextField", "name" : "num2", "flex" : "WHLRTB" }, "selected" : false }, { "nodes" : [ ], "frame" : "{{119, 156}, {77, 43}}", "class" : "TextField", "attributes" : { "uuid" : "EB343BBB-5CFB-4AE7-8BCC-41EE1ACDE42E", "font_size" : 17, "frame" : "{{60, 224}, {200, 32}}", "action" : "", "alignment" : "right", "autocorrection_type" : "default", "text" : "", "font_name" : "<System>", "spellchecking_type" : "default", "class" : "TextField", "name" : "num3", "flex" : "WHLRTB" }, "selected" : false }, { "nodes" : [ ], "frame" : "{{204, 54}, {97, 43}}", "class" : "Label", "attributes" : { "uuid" : "F3870D2E-9FCF-4C83-B5AF-C393166D3D4E", "flex" : "WHLRTB", "corner_radius" : 0, "frame" : "{{85, 224}, {150, 32}}", "number_of_lines" : 0, "border_width" : 1, "border_color" : "RGBA(0.000000,0.000000,0.000000,1.000000)", "alignment" : "center", "text" : "0", "font_name" : "<System>", "class" : "Label", "name" : "label1", "font_size" : 18 }, "selected" : false }, { "nodes" : [ ], "frame" : "{{204, 105}, {97, 43}}", "class" : "Label", "attributes" : { "uuid" : "F3870D2E-9FCF-4C83-B5AF-C393166D3D4E", "flex" : "WHLRTB", "corner_radius" : 0, "frame" : "{{85, 224}, {150, 32}}", "number_of_lines" : 0, "border_width" : 1, "border_color" : "RGBA(0.000000,0.000000,0.000000,1.000000)", "alignment" : "center", "text" : "0", "font_name" : "<System>", "class" : "Label", "name" : "label2", "font_size" : 18 }, "selected" : false }, { "nodes" : [ ], "frame" : "{{204, 156}, {97, 43}}", "class" : "Label", "attributes" : { "flex" : "WHLRTB", "uuid" : "F3870D2E-9FCF-4C83-B5AF-C393166D3D4E", "corner_radius" : 0, "frame" : "{{85, 224}, {150, 32}}", "border_color" : "RGBA(0.000000,0.000000,0.000000,1.000000)", "border_width" : 1, "alignment" : "center", "text" : "0", "font_name" : "<System>", "class" : "Label", "name" : "label3", "font_size" : 18 }, "selected" : false }, { "nodes" : [ ], "frame" : "{{95, 207}, {142, 41}}", "class" : "Button", "attributes" : { "action" : "on_button", "flex" : "WHLRTB", "border_width" : 1, "frame" : "{{120, 224}, {80, 32}}", "title" : "submit", "uuid" : "BD6B3517-3570-46B3-B6E4-E967C932197B", "class" : "Button", "corner_radius" : 0, "name" : "button1", "font_size" : 15 }, "selected" : false }, { "nodes" : [ ], "frame" : "{{174, 6}, {73, 19}}", "class" : "Label", "attributes" : { "font_size" : 18, "flex" : "WHLRTB", "frame" : "{{85, 224}, {150, 32}}", "uuid" : "AC87D8D9-39BC-4DA0-90E3-861257E72023", "class" : "Label", "alignment" : "left", "text" : "1", "custom_attributes" : "", "name" : "num_round", "font_name" : "<System>" }, "selected" : false }, { "nodes" : [ ], "frame" : "{{16, 6}, {150, 19}}", "class" : "Label", "attributes" : { "name" : "Round_title", "flex" : "WHLRTB", "frame" : "{{85, 224}, {150, 32}}", "uuid" : "7CBBD8E2-1DAE-471D-B7D6-2EAA890A3C7F", "class" : "Label", "alignment" : "right", "text" : "Round", "font_size" : 18, "font_name" : "<System>" }, "selected" : false }, { "nodes" : [ ], "frame" : "{{16, 33}, {95, 20}}", "class" : "Label", "attributes" : { "flex" : "WHLRTB", "font_name" : "<System>", "frame" : "{{85, 224}, {150, 32}}", "uuid" : "7CBBD8E2-1DAE-471D-B7D6-2EAA890A3C7F", "class" : "Label", "alignment" : "center", "text" : "User name", "name" : "", "font_size" : 18 }, "selected" : false }, { "nodes" : [ ], "frame" : "{{119, 33}, {77, 20}}", "class" : "Label", "attributes" : { "flex" : "WHLRTB", "font_size" : 18, "frame" : "{{85, 224}, {150, 32}}", "uuid" : "7CBBD8E2-1DAE-471D-B7D6-2EAA890A3C7F", "class" : "Label", "alignment" : "center", "text" : "point", "name" : "", "font_name" : "<System>" }, "selected" : false }, { "nodes" : [ ], "frame" : "{{204, 33}, {97, 20}}", "class" : "Label", "attributes" : { "flex" : "WHLRTB", "font_name" : "<System>", "frame" : "{{85, 224}, {150, 32}}", "uuid" : "7CBBD8E2-1DAE-471D-B7D6-2EAA890A3C7F", "class" : "Label", "alignment" : "center", "text" : "total", "font_size" : 18, "name" : "" }, "selected" : false } ], "frame" : "{{0, 0}, {320, 480}}", "class" : "View", "attributes" : { "tint_color" : "RGBA(0.000000,0.478000,1.000000,1.000000)", "enabled" : true, "border_color" : "RGBA(0.000000,0.000000,0.000000,1.000000)", "background_color" : "RGBA(1.000000,1.000000,1.000000,1.000000)", "name" : "", "flex" : "" }, "selected" : false } ]