使用 tkinter.filedialog.askdirectory() 时有一个预加载的目录 [英] have a pre-loaded directory when using tkinter.filedialog.askdirectory()

查看:151
本文介绍了使用 tkinter.filedialog.askdirectory() 时有一个预加载的目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

进口:

from tkinter import *
from tkinter.filedialog import askopenfilename, askdirectory

主要:

master = Tk()
Label(master, text="Output dir").grid(row=1, column=0 ,sticky=W)
with open('{}\\resources\\last_saved_dir.config'.format(path_of_main_project), 'r') as saved_dir:
    last_dir = saved_dir.read().replace('\n', '')
entry_dir=Entry(master, text=last_dir, width=50)
entry_dir.grid(row=1, column=1, sticky=W, padx=5)
Button(master, text="Browse...", width=10, command=lambda:get_ouput_directory(entry_dir)).grid(row=1, column=2, sticky=W)

# The following part is after directory is chosen, runs the rest of the program. 
# Not important for my question, just showing that I run and close my loop.

Button(master, text="Ok",     command=run_and_close, width=10).grid(row=3, column=1, sticky=E, padx=5)
Button(master, text="Cancel", command=close, width=10).grid(row=3, column=2, sticky=W)

master.bind('<Return>', run_and_close)
master.bind('<Escape>', close)

mainloop()

这将创建一个 gui 窗口,提示用户输入一个目录,文本为 Output dir,然后打开一个带有 Browse 按钮以加载目录的输入窗口

This creates a gui window that prompts the user to enter a directory, with the text Output dir and then an open entry window with a Browse button to load the directory

下面是get_ouput_directory()函数:

def get_ouput_directory(directory_entry):
    dir_name = askdirectory(title = "SiteConverter output directory")
    directory_entry.delete(0,END)
    directory_entry.insert(0,dir_name)
    with open('{}\\resources\\last_saved_dir.config'.format(path_of_main_project), 'w') as saved_dir:
        saved_dir.write(dir_name)

想法是:

  1. 用户第一次运行时,输入窗口是空的,用户会浏览一个目录,然后将该目录保存在一个名为 last_saved_dir.config
  2. 的文件中的某个文件夹中
  3. 用户第二次运行程序时,程序会读取该文件并加载窗口内的目录,因此如果目录未更改,则用户不必每次都手动输入.

请注意,程序不会从第二次运行开始读取文件,它总是会读取,但如果用户第一次运行它,第一次将是不需要的目录或空白.但是,每次我运行脚本时,即使最后一个目录保存在 last_saved_dir.config 中,输入窗口也是空白的.当我在 entry_dir=Entry(master, text=last_dir, width=50) 之前添加 print(last_dir) 时,最后一个目录被正确读取并且类型为 <class 'str'> 这应该对 text= ...

Note that the program doesn't start reading the file from second run, it would always read, but the first time would either be an unwanted directory or blank if the user is running it for the first time. However every time I run the script the entry window is blank even though the last directory is saved inside last_saved_dir.config. When I add print(last_dir) just before entry_dir=Entry(master, text=last_dir, width=50) the last directory is read correctly and the the type is <class 'str'> which should be good for text= ...

推荐答案

我不太确定条目的 text= 选项有什么作用,因为它没有被提及in 任何 参考.要将文本放入条目中,请使用 .insert() 方法:

I'm not really sure what the text= option of an Entry does, since it isn't mentioned in any reference. To put text in an entry use the .insert() method:

entry_dir.insert(END, last_dir)

然后,要始终使用当前在 Entry 中的文件夹打开 askdirectory 窗口,请使用

Then, to always open the askdirectory window with the folder currently in the Entry, use

dir_name = askdirectory(title = "SiteConverter output directory", initialdir=directory_entry.get())

<小时>

根据 Bryan Oakley 的 comments,Tkinter 接受选项名称的缩写,只要它们是唯一的.Entry 小部件没有 text 选项,但它有一个 textvariable 选项,因此这是您在设置 text=...<时设置的选项/代码>.这使得如果你要使用它,你不应该给它一个字符串对象,而是一个 StingVar 对象.对于这个特定的例子,你没有它也很好.


As per Bryan Oakley's comments, Tkinter accepts abbreviations of option names as long as they are unique. The Entry widget doesn't have a text option, but it does have a textvariable option so that's the one you set when you set text=.... That makes that if you were to use it, you shouldn't give it a string object but a StingVar object. For this specific example you're good without it though.

这篇关于使用 tkinter.filedialog.askdirectory() 时有一个预加载的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆