简体   繁体   English

选择脚本的输出文件夹后无法关闭Tkinter窗口

[英]Can't close Tkinter window after selecting output folder for script

I have a function like this: 我有一个这样的功能:

def ask_open_directory():
    root = Tk()
    # Prevent an actual interface from showing. We just want the file selection window
    root.withdraw()
    # Set focus on the window. This only works on MacOS
    os.system('''/usr/bin/osascript -e 'tell app "Finder" to set frontmost of process "Python" to true' ''')
    # Open selector
    dirpath = tkinter.filedialog.askdirectory()

    # Doesn't do anything
    root.destroy()

    return dirpath

which is called first to select an input directory, and closes just fine, and then again right after, to select an output directory. 首先调用它来选择输入目录,然后关闭就好了,然后紧接着再次选择输出目录。

The script takes a couple of minutes to churn through all the data, and all the while, the Tkinter window for selecting output directory stays frozen until the script completes. 该脚本需要花费几分钟的时间来浏览所有数据,并且一直以来,用于选择输出目录的Tkinter窗口一直保持冻结状态,直到脚本完成为止。

Eg my script is organized like 例如,我的脚本的组织方式如下

def massive_function():
    input = custom_reader_function(input_location = ask_open_directory())
    output = ask_open_directory()

    lots of stuff happening
    finish

What am I missing? 我想念什么?

What you are trying to achieve does not justify more than on Tkinter.Tk() instance. 您想要实现的目标并不能证明Tkinter.Tk()实例的合理性。

I mean, you should remove root = Tk() , root.withdraw() and root.destroy() from your function. 我的意思是,您应该从函数中删除root = Tk()root.withdraw()root.destroy() You should instantiate Tkinter.Tk() in your main program, not within individual functions. 您应该在主程序中而不是在单个函数中实例化Tkinter.Tk()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM