今日のTkinterの記事では、Tkinterのボタンウィジェットを取り上げます。
Tkinterのボタンウィジェットは、とてもシンプルで使いやすいウィジェットです。
GUIアプリケーションにボタンを作成し、機能を追加する方法を、いくつかの例を使って見ていきましょう!
この記事もチェック:PythonでTkinterを使ってGUIの電卓アプリを作成する方法
Tkinter ボタンウィジェット
このウィジェットを使って、様々な種類のボタンを作ることができます。
テキストだけでなく、画像を含むボタンも作成できます。
また、Tkinter Buttonは、コールバック関数を使って、ボタンの機能に対応する特定の関数を呼び出すことができます。
特定のテキストとコールバック関数を持つボタンを定義するには、以下の定義を使用します。
button = tk.Button(master, text, command)
|
ここで、text はボタン上のテキスト、command はボタンが押されたときに呼び出されるコールバック関数を指します。
それでは、ボタンをクリックすると、それがクリックされたことを知らせるアラートボックスを表示する簡単なアプリケーションを作ってみましょう!
以下のコードには、1行ごとにコメントがついています。
import tkinter as tk
from tkinter import messagebox
# Create the master objectmaster = tk.Tk()
# Sets the window size as "300x300"master.geometry("300x300")
# This is the button callback function# This must be visible to the button, so we must define it before the button widget!def buttonCallback():
messagebox.showinfo("Message", "You have clicked the Button!")
# Create a Button widgetbutton = tk.Button(master, text="Click", command=buttonCallback)
# And a label for itlabel_1 = tk.Label(master, text="Simple Button")
# Use the grid geometry manager to put the widgets in the respective positionlabel_1.grid(row=0, column=0)
button.grid(row=1, column=0)
# The application mainlooptk.mainloop() |
さて、このプログラムを実行すると、次のような出力が得られます。
from PIL import Image, ImageTk
# Create a tk.PhotoImagemy_image = Image.open("path/to/image.png")
# Resize it to fit button dimensionsmy_image = my_image.resize((25, 25), Image.ANTIALIAS)
|
さて、もしボタンと一緒に画像を表示させて、もう少し派手にしたい場合は、それが可能です!
この場合、tk.Button() の呼び出しに image という別のキーワード引数を追加してください。
これは ImageTk.PhotoImage で、画像ファイルを使ってインスタンス化しなければなりません。
ImageTkを使用するには、PILモジュールからインポートする必要があります。
まず、PIL画像を作成し、十分に小さくなるようにリサイズしてから、PhotoImage` を生成します。
my_image = Imagetk.PhotoImage(my_image)
|
さて、これで photoimage オブジェクトを作成する準備ができました。
b = Button(compound=LEFT, image=my_image, text="Button")
|
これでようやく、このオブジェクトをボタンに追加できるようになりました。
b = Button(compound=LEFT, image=my_image, text="Button", compound=tk.TOP)
|
しかし、ちょっと注意が必要です。
アイコンを使うと、ボタンが持っているテキストが上書きされてしまうからです。
これを避けるには、キーワード引数 compound を使用します。
この値には tk.CENTER、tk.LEFT、tk.RIGHT、tk.TOP または tk.BOTTOM を設定することができます。
これは、画像に対するテキストの向きを指定するものです。
テキストは画像の上に配置されるので、 tk.TOP を使用します。
import tkinter as tk
from tkinter import messagebox
from PIL import Image, ImageTk
# Create the master objectmaster = tk.Tk()
# Sets the window size as "300x300"master.geometry("300x300")
# This is the button callback function# This must be visible to the button, so we must define it before the button widget!def buttonCallback():
messagebox.showinfo("Message", "You have clicked the Button!")
# Create a tk.PhotoImagemy_image = Image.open("debian.png")
# Resize it to fit button dimensionsmy_image = my_image.resize((25, 25), Image.ANTIALIAS)
# Finally, get the PhotoImage objectmy_image = ImageTk.PhotoImage(my_image)
# Create a Button widgetbutton = tk.Button(master, text="Click", image=my_image, command=buttonCallback, compound=tk.TOP)
# And a label for itlabel_1 = tk.Label(master, text="Simple Button")
# Use the grid geometry manager to put the widgets in the respective positionlabel_1.grid(row=0, column=0)
button.grid(row=1, column=0)
# The application mainlooptk.mainloop() |
以下、完全なコードを紹介します。

結果は以下の通りです。

まとめ
Tkinterのボタンウィジェットを作ることができましたか?今後のチュートリアルでは、さらにいくつかのウィジェットにフォーカスしていきますので、ご期待ください!
この記事もチェック:TkinterのScaleウィジェットの使い方を解説する