LoginSignup
5
3

More than 3 years have passed since last update.

tkinterで干支を自動算出してくれるプログラムを作ってみた

Last updated at Posted at 2020-07-06
1 / 12

干支自動算出機を作ってみた

  • 最近、なんとなく思ったことや考えたことを調べることが多くて(暇だから)。。。
  • ふいに干支について調べてみました。→ 干支別性格診断発見
  • 案外当たってました。(特徴をうまくつかんでいたように感じた)
  • 干支が分かれば人間関係も良好に築けるのではないかと思い、さらに調べると簡単に求める方法が出てきたのでプログラミングしてみました。[1]

コーディング内容

今回はtkinterを利用してプログラムを作成しました[2]

# coding:utf-8
import tkinter as tk

ウインドウ

自動算出してくれるプログラムを作るにあたって、基礎となるウインドウを作成しました
サイズは画像を挿入するために少し大きめの横1000の縦650に、タイトルを西暦から干支を確認しようという名前に設定しました

root = tk.Tk()
root.geometry("1000x650")
root.title("西暦から干支を確認しよう")

キャンバス・画像の配置

画像を貼り付けるため(create_imageを使用するため)にtkinterにキャンバスを配置し、そして画像を貼り付けました

canvas = tk.Canvas(bg="white", width=1000, height=650)
canvas.place(x = 0, y = 0)
img1 = tk.PhotoImage(file="kadaireport.photo1.png")
img2 = tk.PhotoImage(file="kadaireport.photo2.png")
img3 = tk.PhotoImage(file="kadaireport.photo3.png")
img4 = tk.PhotoImage(file="kadaireport.photo4.png")
img5 = tk.PhotoImage(file="kadaireport.photo5.png")
canvas.create_image(300, 130, image=img1)
canvas.create_image(670, 290, image=img2)
canvas.create_image(300, 430, image=img3)
canvas.create_image(60, 220, image=img4)
canvas.create_image(60, 520, image=img4)
canvas.create_image(920, 370, image=img5)

入力欄・実行ボタンの配置

入力・出力欄はEntryを、ボタンはButtonを使って配置しました

editbox = tk.Entry(width=4, font=("Times", 36))
editbox.place(x = 570, y = 230)

Button = tk.Button(root, text = "診断", font=("Helvetica", 18), command=Push)
Button.place(x = 690, y = 240)

eto_answer = tk.Entry(width=4, font=("Times", 48))
eto_answer.place(x = 220, y = 380)

コメントの表示

対話型にするためにところどころにコメント(セリフ)をLabelを使って配置しました

question1 = tk.Label(text="干支診断ダヨ", bg="white", font=("Helvetica", 18))
question1.place(x = 230, y = 70)
question2 = tk.Label(text="君は何年生まれなんだい?", bg="white", font=("Helvetica", 18))
question2.place(x = 150, y = 140)
end1 = tk.Label(text="です。", bg="white", font=("Helvetica", 18))
end1.place(x = 670, y = 310)
answer = tk.Label(text="だったら君の干支は", bg="white", font=("Helvetica", 18))
answer.place(x = 180, y = 340)
end2 = tk.Label(text="ダネ!!", bg="white", font=("Helvetica", 18))
end2.place(x = 330, y = 460

ボタンが押されたときの操作

まずetoという名のリストを作り、
ボタンが押されたときに干支をリストから算出をしてその結果をテキスト欄に表示するという操作(プログラム)をPushという関数にdefで定義した

eto = ["子年", "丑年", "寅年", "卯年", "辰年", "巳年", "午年", "未年", "申年", "酉年", "戌年", "亥年"]

def Push():
    n = editbox.get()
    amari = ((int(n)+9)%12)
    global eto
    if amari <= 11:
        c = eto[amari-1]
    else:
        c = eto[11]

    eto_answer.delete(0, tk.END)
    eto_answer.insert(tk.END, str(c))

実行結果

2020-07-08.png


完成形

# coding:utf-8
import tkinter as tk

eto = ["子年", "丑年", "寅年", "卯年", "辰年", "巳年", "午年", "未年", "申年", "酉年", "戌年", "亥年"]

def Push():
    n = editbox.get()
    amari = ((int(n)+9)%12)
    global eto
    if amari <= 11:
        c = eto[amari-1]
    else:
        c = eto[11]

    eto_answer.delete(0, tk.END)
    eto_answer.insert(tk.END, str(c))

root = tk.Tk()
root.geometry("1000x650")
root.title("西暦から干支を確認しよう")

canvas = tk.Canvas(bg="white", width=1000, height=650)
canvas.place(x = 0, y = 0)
img1 = tk.PhotoImage(file="kadaireport.photo1.png")
img2 = tk.PhotoImage(file="kadaireport.photo2.png")
img3 = tk.PhotoImage(file="kadaireport.photo3.png")
img4 = tk.PhotoImage(file="kadaireport.photo4.png")
img5 = tk.PhotoImage(file="kadaireport.photo5.png")
canvas.create_image(300, 130, image=img1)
canvas.create_image(670, 290, image=img2)
canvas.create_image(300, 430, image=img3)
canvas.create_image(60, 220, image=img4)
canvas.create_image(60, 520, image=img4)
canvas.create_image(920, 370, image=img5)

editbox = tk.Entry(width=4, font=("Times", 36))
editbox.place(x = 570, y = 230)

Button = tk.Button(root, text = "診断", font=("Helvetica", 18), command=Push)
Button.place(x = 690, y = 240)

eto_answer = tk.Entry(width=4, font=("Times", 48))
eto_answer.place(x = 220, y = 380)

question1 = tk.Label(text="干支診断ダヨ", bg="white", font=("Helvetica", 18))
question1.place(x = 230, y = 70)
question2 = tk.Label(text="君は何年生まれなんだい?", bg="white", font=("Helvetica", 18))
question2.place(x = 150, y = 140)
end1 = tk.Label(text="です。", bg="white", font=("Helvetica", 18))
end1.place(x = 670, y = 310)
answer = tk.Label(text="だったら君の干支は", bg="white", font=("Helvetica", 18))
answer.place(x = 180, y = 340)
end2 = tk.Label(text="ダネ!!", bg="white", font=("Helvetica", 18))
end2.place(x = 330, y = 460)

root.mainloop()

感想

  • フォントは自分がしっくりくるものを使う
  • 次に画像を挿入する場合はもっと良い画像を探したい
  • etoリスト内は文字なはずなのに表示するときにstr関数が必要なのはなんでだろう?
  • Windowsに標準装備されているペイントというアプリケーションは画像の大きさや左右反転などもできる

参考文献

[1]. 「知っておくと便利な計算11選」
[2].「いちばんやさしいPython入門教室」, 大沢文孝著, 株式会社ソーテック社発行
[3]. 「12歳からはじめる ゼロからの Pythonゲームプログラミング教室」 chapter3 , 著者:大槻有一郎 (著), リブロワークスPython部

5
3
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3