今日はPythonのTkinterを使って年齢計算機というGUIアプリケーションを作ります。
さあ、はじめましょう
アプリケーションの名前が、このアプリケーションが行うことのすべてを表しています。
年齢計算機のインターフェイスをデザインする
Tkinterのプロジェクトの最初のステップは、アプリケーションウィンドウの設計です。
この設計には、次の2つのステップがあります。
- カスタムウィンドウの作成
- ウィンドウアプリケーションに要素を追加する
カスタムウィンドウの作成
空のカスタムウィンドウを作成します。
tkinter` モジュールをインポートし、window オブジェクトを作成する必要があります。
そして、ウィンドウアプリケーションの背景色とタイトルを追加します。
また、リサイズ機能を False に設定して、寸法が変わらないようにします。
そのためのコードを以下に示します。
|
1
2
3
4
5
6
|
import tkinter as tk
window = tk.Tk()
window.geometry("400x300")
window.config(bg="#F7DC6F")
window.resizable(width=False,height=False)
window.title('Age Calculator!')
|
必要な要素を全てウィンドウに追加する
次のステップでは、アプリケーションにラベル、入力ボックス、ボタン、テキストボックスをすべて追加します。
1. ラベル
複数のラベルを使用し、それぞれのラベルが異なる目的を果たすようにします。
導入情報のラベル、ユーザーから生年月日を求める入力ボックスのラベルを用意します。
2. 入力ボックス
ユーザーの生年月日を入力するために、3つの入力ボックスを使用することにします。
1つは日付、1つは月、そして最後の1つは生年です。
3. ボタン
このアプリケーションでは、年齢を計算するためのボタンと、アプリケーションを終了するためのボタンの2つを使用する予定です。
4. テキストボックス
計算された年齢を表示するために、1つのテキストボックスのみを使用します。
デザインコードの全体像は以下の通りです。
お好みで変更してください。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
l1 = tk.Label(window,text="The Age Calculator!",font=("Arial", 20),fg="black",bg="#F7DC6F")
l2 = tk.Label(window,font=("Arial",12),text="Enter your birthday which includes the day-month-year.",fg="black",bg="#F7DC6F")
l_d=tk.Label(window,text="Date: ",font=('Arial',12,"bold"),fg="darkgreen",bg="#F7DC6F")
l_m=tk.Label(window,text="Month: ",font=('Arial',12,"bold"),fg="darkgreen",bg="#F7DC6F")
l_y=tk.Label(window,text="Year: ",font=('Arial',12,"bold"),fg="darkgreen",bg="#F7DC6F")
e1=tk.Entry(window,width=5)
e2=tk.Entry(window,width=5)
e3=tk.Entry(window,width=5)
b1=tk.Button(window,text="Calculate Age!",font=("Arial",13),command=get_age)
l3 = tk.Label(window,text="The Calculated Age is: ",font=('Arial',12,"bold"),fg="darkgreen",bg="#F7DC6F")
t1=tk.Text(window,width=5,height=0,state="disabled")
b2=tk.Button(window,text="Exit Application!",font=("Arial",13),command=exit)
|
画面上に要素を配置する
要素を画面上に配置するには、place関数を使用します。
この関数は、要素のxとyの座標を要求して、アイテムを正しい位置に配置します。
以下に、要素を配置するコードを示します。
|
1
2
3
4
5
6
7
8
9
10
11
12
|
l1.place(x=70,y=5)
l2.place(x=10,y=40)
l_d.place(x=100,y=70)
l_m.place(x=100,y=95)
l_y.place(x=100,y=120)
e1.place(x=180,y=70)
e2.place(x=180,y=95)
e3.place(x=180,y=120)
b1.place(x=100,y=150)
l3.place(x=50,y=200)
t1.place(x=240,y=203)
b2.place(x=100,y=230)
|
Tkinter における年齢計算のためのインターフェイス
最終的なアプリケーションのデザインは、以下のようなウィンドウになります。
|
1
2
|
from datetime import date
today = date.today()
|
ボタンに機能を追加する
1. 年齢計算ボタン
年齢を計算するためには、まず、3つの入力ボックスから3つの入力(データ – 月 – 年)を取得する必要があります。
次に、生年月日と現在の日付の差を計算します。
現在の日付を取得するために、datetimeモジュールからdate関数をインポートします。
また、今日の全体の日付を格納するオブジェクトを作成します。
以下はそのコードです。
|
1
2
3
4
5
6
7
8
9
|
def get_age():
d= int(e1.get())
m=int(e2.get())
y=int(e3.get())
age = today.year-y-((today.month, today.day)<(m,d))
t1.config(state='normal')
t1.delete('1.0', tk.END)
t1.insert(tk.END,age)
t1.config(state='disabled')
|
次に、年齢を計算する関数を作成し、年齢を計算するボタンに接続します。
この関数は、3つのエントリを取得し、年齢 (現在と生年月日の差) を計算します。
計算された年齢は、テキストボックスの以前の情報をクリアした後、テキストボックスに挿入されます。
そのためのコードを以下に示します。
|
1
2
|
def exit():
window.destroy()
|
ハイライトされた行は、年齢を計算するコードのメインステートメントです。
2. アプリケーションの終了ボタン
アプリケーションの終了ボタンは、ウィンドウを破棄する関数を作成し、ボタンの宣言に commad 属性を追加するだけです。
終了関数のコードは以下のとおりです。
from datetime import date
today = date.today()
def exit():
window.destroy()
def get_age():
d= int(e1.get())
m=int(e2.get())
y=int(e3.get())
age =today.year-y-((today.month, today.day)<(m,d))
t1.config(state='normal')
t1.delete('1.0', tk.END)
t1.insert(tk.END,age)
t1.config(state='disabled')
import tkinter as tk
window = tk.Tk()
window.geometry("400x300")
window.config(bg="#F7DC6F")
window.resizable(width=False,height=False)
window.title('Age Calculator!')
l1 = tk.Label(window,text="The Age Calculator!",font=("Arial", 20),fg="black",bg="#F7DC6F")
l2 = tk.Label(window,font=("Arial",12),text="Enter your birthday which includes the day-month-year.",fg="black",bg="#F7DC6F")
l_d=tk.Label(window,text="Date: ",font=('Arial',12,"bold"),fg="darkgreen",bg="#F7DC6F")
l_m=tk.Label(window,text="Month: ",font=('Arial',12,"bold"),fg="darkgreen",bg="#F7DC6F")
l_y=tk.Label(window,text="Year: ",font=('Arial',12,"bold"),fg="darkgreen",bg="#F7DC6F")
e1=tk.Entry(window,width=5)
e2=tk.Entry(window,width=5)
e3=tk.Entry(window,width=5)
b1=tk.Button(window,text="Calculate Age!",font=("Arial",13),command=get_age)
l3 = tk.Label(window,text="The Calculated Age is: ",font=('Arial',12,"bold"),fg="darkgreen",bg="#F7DC6F")
t1=tk.Text(window,width=5,height=0,state="disabled")
b2=tk.Button(window,text="Exit Application!",font=("Arial",13),command=exit)
l1.place(x=70,y=5)
l2.place(x=10,y=40)
l_d.place(x=100,y=70)
l_m.place(x=100,y=95)
l_y.place(x=100,y=120)
e1.place(x=180,y=70)
e2.place(x=180,y=95)
e3.place(x=180,y=120)
b1.place(x=100,y=150)
l3.place(x=50,y=200)
t1.place(x=240,y=203)
b2.place(x=100,y=230)
window.mainloop() |
Complete code for Age Calculator in Python
全コードは以下の通りです。

結果は以下の通りです。
これでコーディングの部分はすべて終了です。
アプリケーションを実行してみましょう。
そして、それは完全に動作し、同じことが以下の出力で見ることができます。


まとめ
今日、あなたは年齢計算機を作る方法を学びました。
読んでくれてありがとうございました。