Tkinter는 애플리케이션의 구성 요소와 사용자가 실행할 수 있는 항목을 빌드하기 위해 여러 내장 함수와 클래스 라이브러리 메서드를 제공합니다. 파일 대화 파일/디렉토리 선택 창을 생성하기 위한 클래스 및 라이브러리 기능을 제공하는 tkinter 모듈 중 하나입니다. 파일 대화 상자를 사용할 수 있습니다. 사용자에게 시스템에서 파일이나 디렉토리를 찾아보라고 요청해야 하는 곳입니다.
특정 파일을 선택해야 하는 디렉토리의 위치를 지정할 수도 있습니다. 특정 위치에서 시작하는 파일 대화 상자를 표시하려면 initialdir =
예
사용자에게 시스템 디렉터리에서 파일을 선택하도록 요청하는 응용 프로그램을 만들어 보겠습니다.
# Import required libraries
from tkinter import *
from tkinter import filedialog
from tkinter import ttk
# Create an instance of tkinter window
win = Tk()
win.geometry("700x350")
# Create an instance of style class
style=ttk.Style(win)
def open_win_diag():
# Create a dialog box
file=filedialog.askopenfilename(initialdir="C:/")
f=open(win.file, 'r')
# Create a label widget
label=Label(win, text= "Click the button to browse the file", font='Arial 15 bold')
label.pack(pady= 20)
# Create a button to open the dialog box
button=ttk.Button(win, text="Open", command=open_win_diag)
button.pack(pady=5)
win.mainloop() 출력
위의 코드를 실행하면 두 개의 위젯이 포함된 창이 표시됩니다.

버튼 위젯은 사용자에게 시스템에서 파일을 검색하도록 요청하는 파일 대화 상자를 트리거합니다.

"initialdir=C:/"를 지정했습니다. askopenfilename()에서 기능. 따라서 C 드라이브를 초기 디렉터리로 엽니다.