python --windows打开文件选择器

安装

pip install pywin32

实例

import win32ui

dlg = win32ui.CreateFileDialog(1)  # 参数 1 表示打开文件对话框
dlg.SetOFNInitialDir('C://')  # 设置打开文件对话框中的初始显示目录
dlg.DoModal()
filename = dlg.GetPathName()
print(filename)

方法二:

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)
root.destroy()    # 关闭或用进程的队列方法都可行(不写的话和其他主线程程序会报错)

你可以通过设置 Tk 实例的 attributes ,使其在所有窗口的顶层显示。这里有一个例子:

import tkinter as tk
from tkinter import filedialog

root = tk.Tk()
root.withdraw()  # 隐藏主窗口

# 让文件选择器窗口始终在顶层
root.attributes('-topmost', 1)

directory = filedialog.askdirectory()  # 打开目录选择器
print(directory)
root.destroy()

root.attributes('-topmost', 0)  # 取消窗口置顶

root.attributes('-topmost', 1) 这行代码将主窗口始终显示在最前面。然后在用户选择完路径后,使用 root.attributes('-topmost', 0) 取消置顶。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

像风一样的男人@

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值