gpt4 book ai didi

Python tkinter : browse directory and save to new directory

转载 作者:行者123 更新时间:2023-12-02 00:40:51 26 4
gpt4 key购买 nike

我想通过单击 python tkinker GUI 中的按钮打开一个新的浏览器,需要保存新目录并在 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_directory,这是当前目录。

如果您希望设置一个用户可以在其中选择目录的 GUI,您可以使用:

dir_name = tk.tkFileDialog.askdirectory()

这会将他们选择的目录名称存储在 dir_name 变量中。

有关更多信息,请查看 this link关于如何使用文件对话框。或者,您可以查看一般的 tkinter 文档 here (for Python 2)here (for Python 3) .如果您需要对文件对话框的引用,this是一个很好的来源。

关于Python tkinter : browse directory and save to new directory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46784609/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com