可以在调用 askopenfilename() 方法时,传入 parent 参数,指定父窗口。然后在选择文件后,手动调用父窗口的 destroy() 方法关闭窗口。

代码示例:

import tkinter as tk
from tkinter import filedialog

root = tk.Tk()

def open_file():
    file_path = filedialog.askopenfilename(parent=root)
    print(file_path)
    root.destroy()

button = tk.Button(root, text="Open file", command=open_file)
button.pack()

root.mainloop()
tkinter 当filedialogaskopenfilename运行完后自动关闭窗口

原文地址: https://www.cveoy.top/t/topic/eDrY 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录