我想打开一个新的浏览器点击按钮从python tkinker图形用户界面和新的目录需要保存并显示在图形用户界面上。
我可以用下面的命令打开当前目录;
import os
subprocess.Popen('explorer "C:\temp"')
cur_path = os.path.dirname(__file__)我的问题是,在上述步骤A/B之后,如何保存活动浏览器目录并在GUI上显示?
发布于 2017-10-17 21:26:32
首先,此答案所需的导入:
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_directory,这是当前目录。
如果您希望设置由用户选择目录的GUI,则可以使用:
dir_name = tk.tkFileDialog.askdirectory()这将存储他们在dir_name变量中选择的目录的名称。
有关更多信息,请查看有关如何使用文件对话框的this link。或者,您可以查看一般的tkinter文档here (for Python 2)和here (for Python 3)。如果你需要一个文件对话框的引用,this是一个很好的资源。
https://stackoverflow.com/questions/46784609
复制相似问题