在tkinter打开文件弹出窗口中将“ this pc”指定为初始目录

时间:2018-11-30 23:46:53

标签: python tkinter

我正在编写一个使用tkinter来使用代码打开“打开文件弹出窗口”的python脚本 root.filename = filedialog.askopenfilename(initialdir = "/",title = "Select file",filetypes = (("text files","*.txt"),("all files","*.*")))。我希望它设置initialdir,默认打开的目录为“ This PC”。这可能吗?

编辑:https://www.quora.com/Where-is-exactly-located-the-This-PC-My-Computer-in-Windows似乎无法实现。

1 个答案:

答案 0 :(得分:3)

有可能。

您可以通过CLSID键(即20D04FE0-3AEA-1069-A2D8-08002B30309D)来引用此文件夹,并可以通过以下路径在注册表中查看它:

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 shortcut shell:MyComputerFolder到达此虚拟路径,该路径更有意义且更具可读性:

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', '*.*')))