見出し画像

Pythonista でプログラミング

iOS デバイスの上には Pythonista という Python の実行環境があります。これを使うと実際に Python でプログラムを書いて実行することができるのです。GUIアプリも作れます。
しかし、いちいち iPhone の画面でちまちま入力するわけにもいかず、かといって iCloud 経由でプログラムを送り込む気にもならず、ということで "Hello, world" を一度動かして放置していました。
しかし、時は移り、いまや iPhone の上でも ChatGPT アプリを使うことができます。そこで、ChatGPT に「Pythonista用のアプリを書くことはできますか?」と聞いてみたところ、「はい」というではありませんか。

ということで、ChatGPT アプリを立ち上げて、
「誕生日を入れると星占いの星座を答えるアプリをputhonista 用に生成して。」
とお願いすると、日付の文字列「mm/dd」から星座を返してくれる関数を Python で書いてくれました。ただし実行がコマンドラインインターフェイス(CLI)なのであまり嬉しくありません。あと、星座の名前も日本語を添えてもらいたいですね。

iPhone上の Pythonista内のコマンドラインでの実行の様子

ということで、
「星座を
日本語の星座名(英語の星座名)
という形式で答えるようにして、GUIを使ったプログラムを作成して」
と依頼したら下のようなコードが生成されました。

import ui
import datetime

zodiac_signs = {
    "Aquarius": "みずがめ座",
    "Pisces": "うお座",
    "Aries": "おひつじ座",
    "Taurus": "おうし座",
    "Gemini": "ふたご座",
    "Cancer": "かに座",
    "Leo": "しし座",
    "Virgo": "おとめ座",
    "Libra": "てんびん座",
    "Scorpio": "さそり座",
    "Sagittarius": "いて座",
    "Capricorn": "やぎ座"
}
    
def get_zodiac_sign(month, day):
 
    if (month == 1 and day >= 20) or (month == 2 and day <= 18):
        return "Aquarius"
    elif (month == 2 and day >= 19) or (month == 3 and day <= 20):
        return "Pisces"
    elif (month == 3 and day >= 21) or (month == 4 and day <= 19):
        return "Aries"
    elif (month == 4 and day >= 20) or (month == 5 and day <= 20):
        return "Taurus"
    elif (month == 5 and day >= 21) or (month == 6 and day <= 20):
        return "Gemini"
    elif (month == 6 and day >= 21) or (month == 7 and day <= 22):
        return "Cancer"
    elif (month == 7 and day >= 23) or (month == 8 and day <= 22):
        return "Leo"
    elif (month == 8 and day >= 23) or (month == 9 and day <= 22):
        return "Virgo"
    elif (month == 9 and day >= 23) or (month == 10 and day <= 22):
        return "Libra"
    elif (month == 10 and day >= 23) or (month == 11 and day <= 21):
        return "Scorpio"
    elif (month == 11 and day >= 22) or (month == 12 and day <= 21):
        return "Sagittarius"
    else:
        return "Capricorn"

def calculate_button_tapped(sender):
    birthday = textfield.text
    month, day = map(int, birthday.split("/"))

    zodiac_sign = get_zodiac_sign(month, day)

    result_label.text = "あなたの星座は " + zodiac_signs[zodiac_sign] + "(" + zodiac_sign + ")です!"

view = ui.View(frame=(0, 0, 300, 200))
view.name = "星座判定"
view.background_color = "white"

label = ui.Label(frame=(10, 10, 280, 30))
label.text = "誕生日を入力してください (MM/DD):"
view.add_subview(label)

textfield = ui.TextField(frame=(10, 50, 280, 30))
textfield.border_color = "gray"
textfield.border_width = 1
view.add_subview(textfield)

button = ui.Button(frame=(10, 90, 280, 30))
button.title = "計算"
button.background_color = "lightblue"
button.action = calculate_button_tapped
view.add_subview(button)

result_label = ui.Label(frame=(10, 130, 280, 30))
result_label.text_color = "blue"
view.add_subview(result_label)

view.present('sheet')

これを実行すると無事に以下のようなアプリができました。

GUI版

まあこれでも、まだ誕生日をいちいち文字列で入れなければならないのが面倒ですけれど。

更に指示をすればメニューから月/日を入れられるようにもできるでしょう。

この記事が気に入ったらサポートをしてみませんか?