python实现对话框文件选择以及文件路径选择

python实现对话框文件选择以及文件路径选择


一、效果展现

示例:先给大家看效果,看看是不是你想要的效果。

这里是最终的效果图展示,大家可以看看是不是你需要的效果。

二、步骤

1.引入库

下面展示 代码片

代码如下:

import tkinter as tk
from tkinter import filedialog

2.创建对象

代码如下:

root=tk.Tk()
root.withdraw()

3.调用方法接收文件返回

代码如下(示例):


#FolderPath=filedialog.askdirectory() #如果有特殊需要,非要选择文件夹,这个可以去掉注释使用
FilePath=filedialog.askopenfilename() #一般这个直接选择文件,会比较符合人们的使用习惯和软件的用户体验

print('FolderPath:',FolderPath)
print('FilePath:',FilePath)

最后并且打印出你的路径.


4.全部完整代码


import tkinter as tk
from tkinter import filedialog

root=tk.Tk()
root.withdraw()

#FolderPath=filedialog.askdirectory()  #看情况自己使用
FilePath=filedialog.askopenfilename()

print('FolderPath:',FolderPath)
print('FilePath:',FilePath)

三、总结

到这里就完成了全部的步骤,希望可以对你有用!

猜你喜欢

转载自blog.csdn.net/XRTONY/article/details/113390337