【Python】tkinter로 파일 & 폴더 경로 지정 화면을 작성한다
tkinter란?
tkinter는 Python을 설치할 때 기본적으로 들어있는 표준 모듈입니다. 매우 간단한 코드로 GUI 화면을 만들 수 있습니다. 개인으로 GUI 도구를 만들 때 유용한 모듈입니다.
기본적인 사용법 등은 아래 사이트를 알기 쉽습니다.
Tkinter로 GUI 프로그래밍
파일 및 폴더 참조
tkinter에서 파일 또는 폴더를 참조하는 기능은 filedialog를 사용하여 구현할 수 있습니다. 코드를 호출하는 방법은 다음과 같습니다.
from tkinter import filedialog
이번에는 filedialog의 다음을 사용하여 두 가지 기능을 구현했습니다.
1, askdirectory
디렉토리를 지정하는 기능입니다. 본 기능에 의해 이하 화상의 화면이 열리고, 폴더 패스를 지정하는 것이 가능합니다.

2, askopenfilename
포일을 지정하는 기능입니다. 본 기능에 의해 이하 화상의 화면이 열리고, 파일 패스를 지정하는 것이 가능합니다.

구현
이번에는 다음과 같은 GUI 화면을 만들었습니다. 폴더 경로와 파일 경로를 지정하고 실행 버튼을 누르면 messagebox 기능으로 지정된 경로를 반환합니다.
상단 화면 이미지 다이어그램

실행 결과(messagebox)

코드는 다음과 같습니다.
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 파일을 처리할 수 있습니다.
Reference
이 문제에 관하여(【Python】tkinter로 파일 & 폴더 경로 지정 화면을 작성한다), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/dgkmtu/items/2367a73f7e2d498e6075
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
tkinter에서 파일 또는 폴더를 참조하는 기능은 filedialog를 사용하여 구현할 수 있습니다. 코드를 호출하는 방법은 다음과 같습니다.
from tkinter import filedialog
이번에는 filedialog의 다음을 사용하여 두 가지 기능을 구현했습니다.
1, askdirectory
디렉토리를 지정하는 기능입니다. 본 기능에 의해 이하 화상의 화면이 열리고, 폴더 패스를 지정하는 것이 가능합니다.

2, askopenfilename
포일을 지정하는 기능입니다. 본 기능에 의해 이하 화상의 화면이 열리고, 파일 패스를 지정하는 것이 가능합니다.

구현
이번에는 다음과 같은 GUI 화면을 만들었습니다. 폴더 경로와 파일 경로를 지정하고 실행 버튼을 누르면 messagebox 기능으로 지정된 경로를 반환합니다.
상단 화면 이미지 다이어그램

실행 결과(messagebox)

코드는 다음과 같습니다.
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 파일을 처리할 수 있습니다.
Reference
이 문제에 관하여(【Python】tkinter로 파일 & 폴더 경로 지정 화면을 작성한다), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/dgkmtu/items/2367a73f7e2d498e6075
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
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 파일을 처리할 수 있습니다.
Reference
이 문제에 관하여(【Python】tkinter로 파일 & 폴더 경로 지정 화면을 작성한다), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/dgkmtu/items/2367a73f7e2d498e6075텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)