Python TKinter tkFileDialog.askopenfilename始终位于其他窗口后面
我想创建一个简单的TKinter文件选择对话框,其中包含一个我将从其他脚本而不是更广泛的GUI中使用的函数 我目前的代码是:Python TKinter tkFileDialog.askopenfilename始终位于其他窗口后面,python,tkinter,Python,Tkinter,我想创建一个简单的TKinter文件选择对话框,其中包含一个我将从其他脚本而不是更广泛的GUI中使用的函数 我目前的代码是: # Select a single file and return the full path as a string def select_file(data_dir): chdir(data_dir) root = Tkinter.Tk() root.withdraw() file_path = tkFileDialog.asko
# Select a single file and return the full path as a string
def select_file(data_dir):
chdir(data_dir)
root = Tkinter.Tk()
root.withdraw()
file_path = tkFileDialog.askopenfilename()
return file_path
当我运行此对话框时,文件对话框总是在其他窗口后面。如果我让Spyder最大化,它会在后面打开,所以我必须最小化
有几个问题与此相关,但我无法使任何建议的代码正常工作,因此,如果这被视为重复的问题,我深表歉意
Ben只需在
file\u path=tkFileDialog.askopenfilename()之后使用root.deiconify()
但是在这里创建一个新的Tk
不是一个好主意。使用root.focus\u force()
使根窗口位于顶部,并且文件对话框也应位于顶部:
from Tkinter import *
import tkFileDialog
def select_file(data_dir):
root = Tk()
root.withdraw()
root.focus_force()
return tkFileDialog.askopenfilename(parent=root, initialdir=data_dir)
select_file(data_dir)
您可能希望尝试不撤消根窗口。相反,使用几何体
将其移出屏幕。我从来没有尝试过这个,但问题似乎至少部分与您已撤消根窗口有关。我也有同样的问题。你能解决这个问题吗?嗨,Spyder最大化了它后面仍然打开的对话框。在这和不请求root.draw()之间,我仍然没有在顶部显示窗口。删除root.draw()的附带好处是留下一个工具栏图标,因此您至少可以在不最小化Spyder的情况下最大化对话框。