[Python] Create a file & folder path specification screen with tkinter

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.

What is tkinter

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

File & Folder Reference

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. sample1.PNG 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. sample2.PNG

Implementation

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 ** sample4.PNG ** Execution result (message box) ** sample3.PNG 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()

Summary

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

[Python] Create a file & folder path specification screen with tkinter
[Python] Create a Tkinter program distribution file with cx_Freeze
Create a frame with transparent background with tkinter [Python]
Create a GUI executable file created with tkinter
Create a Photoshop format file (.psd) with python
Create a directory with python
Create a 2d CAD file ".dxf" with python [ezdxf]
File / folder path manipulation in Python
Create a python GUI using tkinter
Create a virtual environment with Python!
Create an Excel file with Python3
Create a binary file in Python
Create a file uploader with Django
[Python] Create a screen for HTTP status code 403/404/500 with Django
Read a file in Python with a relative path from the program
Get a list of files in a folder with python without a path
Create a Python function decorator with Class
Creating a simple PowerPoint file with Python
Build a blockchain with Python ① Create a class
Create a dummy image with Python + PIL.
[Python] Create a virtual environment with Anaconda
Let's create a free group with Python
Quickly create an excel file with Python #python
Create a GUI app with Python's Tkinter
Create a large text file with shellscript
Create a VM with a YAML file (KVM)
Create Excel file with Python + similarity matrix
Create a word frequency counter with Python 3.4
Create a deb file from a python package
[GPS] Create a kml file in Python
I made a configuration file with Python
A memo organized by renaming the file names in the folder with python
How to read a CSV file with Python 2/3
Create a GIF file using Pillow in Python
Create a LINE BOT with Minette for Python
How to create a JSON file in Python
Create a PDF file with a random page size
Create a virtual environment with conda in Python
Create a page that loads infinitely with python
[Note] Create a one-line timezone class with python
You can easily create a GUI with Python
Create a python3 build environment with Sublime Text3
[Python] I made a Youtube Downloader with Tkinter.
Create a matrix with PythonGUI (tkinter combo box)
Create a color bar with Python + Qt (PySide)
Develop Windows apps with Python 3 + Tkinter (exe file)
Steps to create a Twitter bot with python
Create a decision tree from 0 with Python (1. Overview)
Create a new page in confluence with Python
Create a color-specified widget with Python + Qt (PySide)
Create a MIDI file in Python using pretty_midi
Create a Python console application easily with Click
Read line by line from a file with Python
I want to write to a file with Python
Open a file dialog with a python GUI (tkinter.filedialog)
Create a cylinder with open3d + STL file output
How to drop Google Docs in one folder in a .txt file with python
Create a Python module
Create a Python environment
Create a native GUI app with Py2app and Tkinter
Try to dynamically create a Checkbutton with Python's Tkinter