Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PythonTkinter:浏览目录并保存到新目录_Python_Python 3.x - Fatal编程技术网

PythonTkinter:浏览目录并保存到新目录

PythonTkinter:浏览目录并保存到新目录,python,python-3.x,Python,Python 3.x,我想通过点击PythonTkinkerGUI上的按钮打开一个新的浏览器,新的目录需要保存并显示在GUI上 我可以用下面的命令打开当前目录 import os subprocess.Popen('explorer "C:\temp"') cur_path = os.path.dirname(__file__) 我的问题是如何在上述步骤A/B之后保存活动浏览器目录并在GUI上显示?首先,此答案所需的导入: import os import tkinter as tk # if using Pyth

我想通过点击PythonTkinkerGUI上的按钮打开一个新的浏览器,新的目录需要保存并显示在GUI上

我可以用下面的命令打开当前目录

import os
subprocess.Popen('explorer "C:\temp"')
cur_path = os.path.dirname(__file__)

我的问题是如何在上述步骤A/B之后保存活动浏览器目录并在GUI上显示?

首先,此答案所需的导入:

import os
import tkinter as tk # if using Python 3
import Tkinter as tk # if using Python 2
假设您的按钮已定义

以下是获取当前目录的一些示例代码:

curr_directory = os.getcwd() # will get current working directory
如果要设置GUI以要求用户选择文件,请使用:

name = tkinter.tkFileDialog.askopenfilename(initialdir = curr_directory,title = "Select file",filetypes = (("jpeg files","*.jpg"),("all files","*.*")))
print(name)
它将存储他们选择的文件,并将其起始目录设置为当前目录
curr\u directory

如果您希望设置用户选择目录的GUI,则可以使用:

dir_name = tk.tkFileDialog.askdirectory()
这将存储他们在
dir\u name
变量中选择的目录名

有关详细信息,请查看如何使用“文件”对话框。或者,您可以查看常规
tkinter
文档和。如果您需要文件对话框的引用,这是一个很好的源代码