Python 3.x 使用filedialog时隐藏tk窗口
我正在尝试使用Python 3.x 使用filedialog时隐藏tk窗口,python-3.x,tkinter,Python 3.x,Tkinter,我正在尝试使用filedialog.asksavefilename获取保存文件路径。我在空闲的shell中运行这段代码,它是一个基于文本的界面。这是获取保存路径的函数: def getPath(): root=tk.Tk() root.lift() root.attributes('-topmost',True) root.after_idle(root.attributes,'-topmost',False) path = filedialog.asks
filedialog.asksavefilename
获取保存文件路径。我在空闲的shell中运行这段代码,它是一个基于文本的界面。这是获取保存路径的函数:
def getPath():
root=tk.Tk()
root.lift()
root.attributes('-topmost',True)
root.after_idle(root.attributes,'-topmost',False)
path = filedialog.asksaveasfilename(defaultextension=".txt", filetypes=(("Text Documents", "*.txt"),))
root.destroy()
该对话框在其他窗口后面打开,因此我通常将该对话框显示在前面。这是可行的,但它后面还有一扇空窗户,我不想要。我尝试过使用root.draw()
,但这只是隐藏了一切。我希望只打开文件对话框,而不打开空的tk窗口。有什么办法吗
from tkinter import Tk
from tkinter.filedialog import asksaveasfilename
def get_path():
root = Tk()
root.withdraw()
path = asksaveasfilename()
root.destroy()
return(path)
print(get_path()) # to verify expected result
这就是你想要的行为吗?希望这有帮助。谢谢你的建议,我已经尝试过了,但是使用
root.draw
会导致文件对话框不出现