今回は、積分値を単語に変換するPythonのGUIプロジェクトの作り方を見ていきましょう。
さっそくプロジェクトを始めてみましょう。
Step 1: モジュールのインポート
まず、プログラムに必要なモジュールやライブラリをすべてインポートすることから始めます。
GUIウィンドウを作成するために、tkinter モジュールをインポートします。
また、num2words` モジュールをインポートして、number to words 機能を実現します。
|
1
2
|
import num2words as n2w
from tkinter import *
|
ステップ2:GUIウィンドウの作成
さて、基本的な関数を使用して基本的なTkinterウィンドウを作成し、いくつかの基本的なウィジェットを画面に追加していきます。
その中には、ラベル、入力ボックス、ボタンが含まれます。
もしTkinterのウィジェットについて知らないのであれば、以下のチュートリアルを参照してください。
-
- Tkinter GUIウィジェット – 完全なリファレンス
- Tkinterエントリーウィジェット
- Tkinterテキストウィジェット(Tkinter Scrollbar付き
- Tkinterフレームとラベル 簡単なリファレンス
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
root = Tk()
root.title("Numbers tdo Words")
root.geometry("650x400")
num = StringVar()
title = Label(root, text="Number to Words converter",fg="Blue", font=("Arial", 20, 'bold')).place(x=220, y=10)
formats_lable = Label(root, text="Formats supported : ",fg="green", font=("Arial", 10, 'bold')).place(x=100, y=70)
pos_format_lable = Label(root, text="1. Positives : ",fg="green", font=("Arial", 10, 'bold')).place(x=200, y=90)
neg_format_lable = Label(root, text="2. Negatives ",fg="green", font=("Arial", 10, 'bold')).place(x=200, y=110)
float_format_lable = Label(root, text="3. Zeros ",fg="green", font=("Arial", 10, 'bold')).place(x=200, y=130)
zero_format_lable = Label(root, text="4. Floating points/decimals/fractions ",fg="green", font=("Arial", 10, 'bold')).place(x=200, y=150)
num_entry_lable = Label(root, text="Enter a number :",fg="Blue", font=("Arial", 15, 'bold')).place(x=50, y=200)
num_entry = Entry(root,textvariable=num,width=30).place(x=220, y=200)
btn = Button(master=root, text="calculate",fg="green",
font=("Arial", 10, 'bold'),command=num_to_words).place(x=280,y=230)
display = Label(root, text="",fg="black", font=("Arial", 10, 'bold'))
display.place(x=10, y=300)
root.mainloop() |
行21で、ボタンのcommand属性があることに気がついたかもしれません。
この属性はnum_to_words` に設定されていますが、関数を宣言していません。
ステップ3: num_to_words 関数
この関数では、まずユーザから与えられた入力を読み取り、次に num2words 関数を用いて読み取った値を単語に変換し、最後に表示値を計算された数値の単語形式に変更します。
|
1
2
3
4
|
def num_to_words():
given_num = float(num.get())
num_in_word = n2w.num2words(given_num)
display.config(text=str(num_in_word).capitalize())
|
Python Tkinter を使って数字を単語に変換する
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
import num2words as n2w
from tkinter import *
def num_to_words():
given_num = float(num.get())
num_in_word = n2w.num2words(given_num)
display.config(text=str(num_in_word).capitalize())
root = Tk()
root.title("Numbers tdo Words")
root.geometry("650x400")
num = StringVar()
title = Label(root, text="Number to Words converter",fg="Blue", font=("Arial", 20, 'bold')).place(x=220, y=10)
formats_lable = Label(root, text="Formats supported : ",fg="green", font=("Arial", 10, 'bold')).place(x=100, y=70)
pos_format_lable = Label(root, text="1. Positives : ",fg="green", font=("Arial", 10, 'bold')).place(x=200, y=90)
neg_format_lable = Label(root, text="2. Negatives ",fg="green", font=("Arial", 10, 'bold')).place(x=200, y=110)
float_format_lable = Label(root, text="3. Zeros ",fg="green", font=("Arial", 10, 'bold')).place(x=200, y=130)
zero_format_lable = Label(root, text="4. Floating points/decimals/fractions ",fg="green", font=("Arial", 10, 'bold')).place(x=200, y=150)
num_entry_lable = Label(root, text="Enter a number :",fg="Blue", font=("Arial", 15, 'bold')).place(x=50, y=200)
num_entry = Entry(root,textvariable=num,width=30).place(x=220, y=200)
btn = Button(master=root, text="calculate",fg="green",
font=("Arial", 10, 'bold'),command=num_to_words).place(x=280,y=230)
display = Label(root, text="",fg="black", font=("Arial", 10, 'bold'))
display.place(x=10, y=300)
root.mainloop() |

Some Sample Outputs
Fake Tags
Fake tag
Fake tag
—Fake tag
まとめ
このチュートリアルを読んで、コンセプトを理解し、アウトプットを楽しんでいただければ幸いです。
このチュートリアルをお読みいただき、ありがとうございました。
ハッピーラーニング!
Also Read:
- Python Tkinter プロジェクト。携帯電話番号ランダム生成器
- Python Tkinterを使ったYouTube動画ダウンローダー
- Tkinterの目覚まし時計 – ステップバイステップガイド
- Python Tkinter: シンプルな貯金計算機