LoginSignup
1
10

More than 5 years have passed since last update.

【Python】Tkinterを使ってGUIツールをつくってみた

Posted at

どのプラットフォーム(OS)でも使えるということで、身の回りのツール類をPythonに移行しようと思ってる。
とはいえ、VBSなどで作ってるツールは入力画面などGUI使ったりもしている。
手始めに、PythonでGUI系ライブラリの使い方などについて備忘がてらまとめてみる。

なお、今回は以下のサイトを参考に進める。
Tkinter による GUI プログラミング

また、私はmacOSを使用しているので、動作確認等はmacOSで実施している。
※時間があれば、WindowsやLinuxなどでも動作検証させたいという思いはある

1.PythonのGUIライブラリ

GUIライブラリといってもたくさんある。
※情報ソース:Python、GUIライブラリの紹介

どれで試そうか迷った結果、 Tkinter でやることにした。
選んだ理由としては、

  1. よくPythonの参考書で見かけるから
  2. Pythonに標準で付属されているGUIライブラリだから

ただし、見た目が良くないなどのコメントも見るので、
後々他のライブラリも触ってみようかと思っている。
とりあえず今回は、Tkinter にしました。

2. Tkinterとは

Tkinter は Tk を Python から使うためのモジュール。
※Tkは、GUIを作成するためのツールキット
Tkinterは、Pythonの標準で付属されている。
Windows、macOS、Linuxなど様々なプラットフォーム上で利用することができる。

3. Tkinterの導入方法

Tkinterは、標準にPythonに付属されているので、Pythonさえ使える環境であれば使用できる。
※今回は、Pythonの導入方法までは触れない

私の使用しているPythonのVersion。

(env001) Hoge-MBA:bin Hoge$ python --version
Python 3.6.4

モジュールが含まれていることを確認。

(env001) Hoge-MBA:bin Hoge$ python
Python 3.6.4 (default, Jan  6 2018, 11:51:59) 
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> help('modules')

Please wait a moment while I gather a list of all available modules...

(中略)

_multibytecodec     csv                 parser              threading
_multiprocessing    ctypes              pathlib             time
_opcode             curses              pdb                 timeit
_operator           datetime            pickle              tkinter  <=== ★含まれている
_osx_support        dateutil            pickletools         token
_pickle             dbm                 pip                 tokenize

(中略)

_warnings           gzip                s3transfer          zlib
_weakref            hashlib             sched               

Enter any module name to get more help.  Or, type "modules spam" to search
for modules whose name or summary contain the string "spam".

>>> 
>>> 

念のため、ちゃんど動作するかも確認。

(env001) Hoge-MBA:bin Hoge$ python
Python 3.6.4 (default, Jan  6 2018, 11:51:59) 
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> import tkinter
>>> tkinter._test()
>>> 

実行すると以下が表示された。

スクリーンショット 2018-06-09 13.38.31.png

1
10
0

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
1
10