非UI版,直接弹出选择界面
import tkinter as tk
from tkinter import filedialog, StringVar
def check_filename():
root = tk.Tk()
root.withdraw()
filename = filedialog.askopenfilename()
return filename.split('/')[-1] if len(filename) else ''
print(check_filename())
UI版,通过按钮点击
import tkinter as tk
from tkinter import filedialog, StringVar
def check_filename_ui():
root = tk.Tk()
root.title('请选择文件')
path = StringVar()
filename = ['']
def check():
temp = filedialog.askopenfilename()
filename[0] = temp.split('/')[-1] if len(temp) else ''
path.set(filename[0])
tk.Label(root, text="目标路径:").pack()
tk.Entry(root, textvariable=path).pack(fill=tk.BOTH, expand=tk.YES)
tk.Button(root, text="选择文件", command=check).pack()
tk.Button(text="确认并关闭", command=root.destroy).pack()
root.mainloop()
return filename[0]
print(check_filename_ui())
可视化拖拽布局助手
http://xfxuezhang.cn/web/tkinter-helper/
评论 (0)