File dialog (filedialog.askopenfilename) จะได้ focus อัตโนมัติหาก parent window ถูกสร้างก่อนและเป็นหน้าต่างหลัก
ตัวอย่างโค้ด
from tkinter import *
from tkinter import filedialog
from PIL import Image, ImageTk
win = Tk()
win.geometry("700x350")
win.title("File Explorer")
def open_file():
win.filename = filedialog.askopenfilename(
title="Select the file",
filetypes=(("jpg files", "*.jpg"), ("all files", "*.*"))
)
Button(win, text="Open", command=open_file).place(relx=.5, rely=.5, anchor=CENTER)
win.mainloop()
ผลลัพธ์
กดปุ่ม Open จะเปิด file dialog ให้เลือกไฟล์

