In tkinter, I created a screen for specifying attachments and folders. It is useful to be able to specify files and folders with the GUI when creating various tools.
tkinter is a standard module that is included by default when you install Python. You can create a GUI screen with very simple code. This is a convenient module for personal GUI tools.
The following sites are easy to understand for basic usage. GUI programming with Tkinter
The function to refer to a file or folder with tkinter can be implemented by using filedialog. The way to call it in the code is as follows.
from tkinter import filedialog
This time, I implemented two functions using the following of filedialog.
1、askdirectory
It is a function to specify a directory. This function opens the screen of the image below, and you can specify the folder path.
 2、askopenfilename
It is a function to specify the foil. This function opens the screen of the image below, and you can specify the file path.
2、askopenfilename
It is a function to specify the foil. This function opens the screen of the image below, and you can specify the file path.

This time, I created the following GUI screen. If you specify the folder path and file path and press the execute button, the path specified by the messagebox function will be returned.
** Top screen image **
 ** Execution result (message box) **
** Execution result (message box) **
 The code is below.
The code is below.
tkinter_sample.py
import os,sys
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
from tkinter import filedialog
#Folder specification function
def dirdialog_clicked():
    iDir = os.path.abspath(os.path.dirname(__file__))
    iDirPath = filedialog.askdirectory(initialdir = iDir)
    entry1.set(iDirPath)
#File-specified function
def filedialog_clicked():
    fTyp = [("", "*")]
    iFile = os.path.abspath(os.path.dirname(__file__))
    iFilePath = filedialog.askopenfilename(filetype = fTyp, initialdir = iFile)
    entry2.set(iFilePath)
#Execution function when the execute button is pressed
def conductMain():
    text = ""
    
    dirPath = entry1.get()
    filePath = entry2.get()
    if dirPath:
        text += "Folder path:" + dirPath + "\n"
    if filePath:
        text += "File Path:" + filePath
    if text:
        messagebox.showinfo("info", text)
    else:
        messagebox.showerror("error", "No path is specified.")
if __name__ == "__main__":
    #Create root
    root = Tk()
    root.title("sample")
    #Creating Frame1
    frame1 = ttk.Frame(root, padding=10)
    frame1.grid(row=0, column=1, sticky=E)
    #Creating a "folder reference" label
    IDirLabel = ttk.Label(frame1, text="Refer to folder >>", padding=(5, 2))
    IDirLabel.pack(side=LEFT)
    #Creating a "Browse for Folder" entry
    entry1 = StringVar()
    IDirEntry = ttk.Entry(frame1, textvariable=entry1, width=30)
    IDirEntry.pack(side=LEFT)
    #Create "Browse Folder" button
    IDirButton = ttk.Button(frame1, text="reference", command=dirdialog_clicked)
    IDirButton.pack(side=LEFT)
    #Creating Frame2
    frame2 = ttk.Frame(root, padding=10)
    frame2.grid(row=2, column=1, sticky=E)
    #Creating a "file reference" label
    IFileLabel = ttk.Label(frame2, text="File reference >>", padding=(5, 2))
    IFileLabel.pack(side=LEFT)
    #Creating a "file reference" entry
    entry2 = StringVar()
    IFileEntry = ttk.Entry(frame2, textvariable=entry2, width=30)
    IFileEntry.pack(side=LEFT)
    #Creating a "File Browse" button
    IFileButton = ttk.Button(frame2, text="reference", command=filedialog_clicked)
    IFileButton.pack(side=LEFT)
    #Creating Frame3
    frame3 = ttk.Frame(root, padding=10)
    frame3.grid(row=5,column=1,sticky=W)
    #Installation of execute button
    button1 = ttk.Button(frame3, text="Run", command=conductMain)
    button1.pack(fill = "x", padx=30, side = "left")
    #Installation of cancel button
    button2 = ttk.Button(frame3, text=("close"), command=quit)
    button2.pack(fill = "x", padx=30, side = "left")
    root.mainloop()
I introduced the function to specify the folder and file path by the file dialog function of tkinter. If you connect a processing module to this module, you can process any folder or file.
Recommended Posts