Skip to main content
 首页 » 编程设计

python之从 python 脚本自动关闭输出窗口

2025年05月04日14lexus

我写了一个小脚本来批量绘制一些数据。如果没有显示新创建的绘图打开的窗口,则此脚本无法运行。它停止运行,直到窗口被手动关闭,然后脚本继续运行。所以我的问题如下:

有没有办法在 python 脚本中自动关闭输出窗口?

这是脚本

import pynbody 
import numpy as np 
import pynbody.plot.sph as sph 
 
f = open('files.txt', 'r') 
for line in f: 
     line = line[0:-1] 
     s = pynbody.load(line) 
     sph.image(s.gas,qty="temp",width=16, filename=line[0:-6]+line[-5:len(line)],  units='K',  cmap="YlOrRd",  ret_im=False , approximate_fast=False) 
 
#At the end of the loop I would like to close the output window so the script  
#will continue to run  

pynbody.plot.sph 包似乎应该能够在 sph.image 函数调用中打开和关闭输出窗口,但我已经用尽了所有的可能性,现在正在求助于它来运行脚本。

请您参考如下方法:

我使用这个 link 找到了答案

view and then close the figure automatically in matplotlib?

基本上,这是答案:

plt.show(block=False) # block = False allows automatic closing of the image 
# the following command will close the graph in 3 seconds 
time.sleep(3) 
plt.close()