Tkinter는 기능적이고 특징적인 애플리케이션을 만들고 개발하는 데 사용되는 표준 Python 라이브러리입니다. 응용 프로그램의 논리를 구성하는 데 사용할 수 있는 다양한 기능, 모듈 및 패키지가 내장되어 있습니다.
tkFileDialog 시스템 파일 및 디렉토리와 상호 작용하는 데 유용한 Tkinter 라이브러리에서 사용할 수 있는 내장 모듈입니다. 그러나 tkFileDialog를 사용하여 읽기 모드에서 특정 파일을 선택하면 , 잠재적으로 파일에서 사용 가능한 정보를 처리하는 데 추가로 사용될 수 있습니다.
응용 프로그램에 로드될 때 파일의 절대 경로에 액세스하려면 OS 모듈의 사용 가능한 기능, 즉 os.path.abspath(file.name)를 사용할 수 있습니다. 기능. 이 함수는 창이나 화면에 표시하기 위해 변수에 저장할 수 있는 파일의 절대 경로를 반환합니다.
예
# Import the required Libraries
from tkinter import *
from tkinter import ttk, filedialog
from tkinter.filedialog import askopenfile
import os
# Create an instance of tkinter frame
win = Tk()
# Set the geometry of tkinter frame
win.geometry("700x350")
def open_file():
file = filedialog.askopenfile(mode='r', filetypes=[('Python Files', '*.py')])
if file:
filepath = os.path.abspath(file.name)
Label(win, text="The File is located at : " + str(filepath), font=('Aerial 11')).pack()
# Add a Label widget
label = Label(win, text="Click the Button to browse the Files", font=('Georgia 13'))
label.pack(pady=10)
# Create a Button
ttk.Button(win, text="Browse", command=open_file).pack(pady=20)
win.mainloop() 출력
코드를 실행하면 먼저 다음 창이 표시됩니다. -

이제 "찾아보기" 버튼을 클릭하고 탐색기에서 Python 파일을 선택합니다. 선택한 파일의 절대 경로가 표시됩니다.
