LoginSignup
35
33

More than 3 years have passed since last update.

【Python】tkinterでファイル&フォルダパス指定画面を作成する

Posted at

tkinterで、添付ファイル指定とフォルダ指定の画面を作成しました。様々なツールを作るうえでファイルやフォルダをGUIで指定できると便利なので重宝してます。

tkinterとは

tkinterはPythonをインストールした際にデフォルトで入っている標準モジュールです。非常にシンプルなコードでGUI画面を作成することができます。個人でGUIツールを作成する際は便利なモジュールです。

基本的な使い方等は下記のサイトが分かりやすいです。
Tkinter による GUI プログラミング

ファイル&フォルダ参照

tkinterでファイルorフォルダを参照する機能は、filedialogを使うと実装可能です。コード上の呼び出し方は以下の通りです。

from tkinter import filedialog

今回はfiledialogの以下を使って2つの機能を実装しました。
1、askdirectory
ディレクトリを指定する機能です。本機能によって以下画像の画面が開き、フォルダパスを指定することが可能です。
sample1.PNG
2、askopenfilename
フォイルを指定する機能です。本機能によって以下画像の画面が開き、ファイルパスを指定することが可能です。
sample2.PNG

実装

今回は以下のようなGUI画面を作成しました。フォルダパスとファイルパスを指定し、実行ボタンを押せばmessagebox機能で指定されたパスを返却します。
トップ画面イメージ図
sample4.PNG
実行結果(messagebox)
sample3.PNG
コードは以下の通りです。

tkinter_sample.py

import os,sys
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
from tkinter import filedialog

# フォルダ指定の関数
def dirdialog_clicked():
    iDir = os.path.abspath(os.path.dirname(__file__))
    iDirPath = filedialog.askdirectory(initialdir = iDir)
    entry1.set(iDirPath)

# ファイル指定の関数
def filedialog_clicked():
    fTyp = [("", "*")]
    iFile = os.path.abspath(os.path.dirname(__file__))
    iFilePath = filedialog.askopenfilename(filetype = fTyp, initialdir = iFile)
    entry2.set(iFilePath)

# 実行ボタン押下時の実行関数
def conductMain():
    text = ""

    dirPath = entry1.get()
    filePath = entry2.get()
    if dirPath:
        text += "フォルダパス:" + dirPath + "\n"
    if filePath:
        text += "ファイルパス:" + filePath

    if text:
        messagebox.showinfo("info", text)
    else:
        messagebox.showerror("error", "パスの指定がありません。")

if __name__ == "__main__":

    # rootの作成
    root = Tk()
    root.title("サンプル")

    # Frame1の作成
    frame1 = ttk.Frame(root, padding=10)
    frame1.grid(row=0, column=1, sticky=E)

    # 「フォルダ参照」ラベルの作成
    IDirLabel = ttk.Label(frame1, text="フォルダ参照>>", padding=(5, 2))
    IDirLabel.pack(side=LEFT)

    # 「フォルダ参照」エントリーの作成
    entry1 = StringVar()
    IDirEntry = ttk.Entry(frame1, textvariable=entry1, width=30)
    IDirEntry.pack(side=LEFT)

    # 「フォルダ参照」ボタンの作成
    IDirButton = ttk.Button(frame1, text="参照", command=dirdialog_clicked)
    IDirButton.pack(side=LEFT)

    # Frame2の作成
    frame2 = ttk.Frame(root, padding=10)
    frame2.grid(row=2, column=1, sticky=E)

    # 「ファイル参照」ラベルの作成
    IFileLabel = ttk.Label(frame2, text="ファイル参照>>", padding=(5, 2))
    IFileLabel.pack(side=LEFT)

    # 「ファイル参照」エントリーの作成
    entry2 = StringVar()
    IFileEntry = ttk.Entry(frame2, textvariable=entry2, width=30)
    IFileEntry.pack(side=LEFT)

    # 「ファイル参照」ボタンの作成
    IFileButton = ttk.Button(frame2, text="参照", command=filedialog_clicked)
    IFileButton.pack(side=LEFT)

    # Frame3の作成
    frame3 = ttk.Frame(root, padding=10)
    frame3.grid(row=5,column=1,sticky=W)

    # 実行ボタンの設置
    button1 = ttk.Button(frame3, text="実行", command=conductMain)
    button1.pack(fill = "x", padx=30, side = "left")

    # キャンセルボタンの設置
    button2 = ttk.Button(frame3, text=("閉じる"), command=quit)
    button2.pack(fill = "x", padx=30, side = "left")

    root.mainloop()

まとめ

tkinterのfiledialog機能によってフォルダとファイルのパスを指定する機能をご紹介しました。このモジュールに処理モジュールを繋げれば、任意のフォルダorファイルを処理することが可能となります。

35
33
1

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
35
33