指定“这台电脑"作为 tkinter 打开文件弹出窗口中的初始目录

编程入门 行业动态 更新时间:2024-10-25 02:20:56
本文介绍了指定“这台电脑"作为 tkinter 打开文件弹出窗口中的初始目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送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:关键词]

本文发布于:2023-04-30 07:12:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1390789.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:弹出   这台   文件   目录   窗口中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!