Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách tạo thư mục mới bằng hộp thoại askdirectory trong Tkinter

Dùng filedialog.askdirectory() chọn thư mục cha, os.makedirs() tạo thư mục con.

Ví dụ

from tkinter import *
from tkinter import ttk
from tkinter import filedialog
import os

win = Tk()
win.geometry("700x350")

def create_subfolder():
    source_path = filedialog.askdirectory(title='Select the Parent Directory')
    path = os.path.join(source_path, 'Images')
    os.makedirs(path)

ttk.Button(win, text="Select a Folder", command=create_subfolder).pack(pady=5)

win.mainloop()

Kết quả

Click button → hộp thoại chọn thư mục → tự tạo thư mục "Images" bên trong.

Cách tạo thư mục mới bằng hộp thoại askdirectory trong Tkinter

Cách tạo thư mục mới bằng hộp thoại askdirectory trong Tkinter