问题描述
限时送ChatGPT账号..我正在编写一个 python 脚本,它使用 tkinter 使用代码打开打开的文件弹出窗口"root.filename = filedialog.askopenfilename(initialdir = "/",title = "选择文件",filetypes = (("文本文件","*.txt"),("所有文件","*.*")))
.我希望它设置initialdir,打开的目录默认为This PC".这可能吗?
I'm writing a python script that uses tkinter to open a "open file pop up" using the code
root.filename = filedialog.askopenfilename(initialdir = "/",title = "Select file",filetypes = (("text files","*.txt"),("all files","*.*")))
. I want it to set the initialdir, the directory that is open default to "This PC". Is this possible?
https://www.quora/Where-is-exactly-located-the-This-PC-My-Computer-in-Windows 似乎说这是不可能的.
https://www.quora/Where-is-exactly-located-the-This-PC-My-Computer-in-Windows appears to say that this is not possible.
推荐答案
有可能.
您可以通过 CLSID
键引用此文件夹,即 20D04FE0-3AEA-1069-A2D8-08002B30309D
,可以通过以下路径在注册表中查看:
You can refer to this folder by the CLSID
key, which is 20D04FE0-3AEA-1069-A2D8-08002B30309D
and can be viewed in the Registry by this path:
HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{20D04FE0-3AEA-1069-A2D8-08002B30309D}
试试这个:
import tkinter as tk
import tkinter.filedialog as filedialog
root = tk.Tk()
root.withdraw()
root.filename = filedialog.askopenfilename(initialdir='::{20D04FE0-3AEA-1069-A2D8-08002B30309D}',
title='Select file',
filetypes=(('text files', '*.txt'), ('all files', '*.*')))
或者,您可以通过 shell 快捷方式 shell 访问此虚拟路径:MyComputerFolder
,更有意义和可读性:
Alternatively, you can reach this virtual path by the shell shortcut shell:MyComputerFolder
, which is both more meaningful and readable:
import tkinter as tk
import tkinter.filedialog as filedialog
root = tk.Tk()
root.withdraw()
root.filename = filedialog.askopenfilename(initialdir='shell:MyComputerFolder',
title='Select file',
filetypes=(('text files', '*.txt'), ('all files', '*.*')))
这篇关于指定“这台电脑"作为 tkinter 打开文件弹出窗口中的初始目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论