在我创建的tkinter gui中,如果在tkinter窗口中打开matplotlib图时尝试编写文本文件,则python会引发以下错误:
Fatal Python error: PyEval_RestoreThread: NULL tstate
Current thread 0x00002f58 (most recent call first):
File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\backends\backend_qt5.py", line 164 in mainloop
File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\backend_bases.py", line 193 in __call__
File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\pyplot.py", line 253 in show
File "C:\Users\rohit\python\connections.py", line 40 in draw_connected_pans
File "tlscript.pyw", line 1325 in draw_all_pans
File "C:\ProgramData\Anaconda3\lib\tkinter\__init__.py", line 1699 in __call__
File "C:\ProgramData\Anaconda3\lib\tkinter\__init__.py", line 1277 in mainloop
File "tlscript.pyw", line 1455 in <module>
如果我关闭包含绘图的tkinter窗口,然后执行用于写入文本文件的gui函数,则不会发生错误
用于创建matplotlib图的功能
函数,如上面的回溯中所示,在connections.py中包含第40行
def draw_connected_pans(G):
Pos = nx.spring_layout(G)
nx.draw_networkx(G, pos = Pos)
plt.axis('off')
plt.show()
函数包含tlscript.py中的第1325行,如上述回溯
def draw_all_pans():
try:
application_window = window
connections.draw_connected_pans(graph)
except:
exceptionhandler(traceback.format_exc())
触发错误的代码
connectionmenu2.add_command(label="Save as html plot of connected entities of pan ", command=save_connection_html)
@mydecorator
def save_connection_html():
try:
if graph:
application_window = window
pan = simpledialog.askstring('Prompt', 'Enter PAN', parent = application_window)
connections.use_d3(graph,pan)
except:
exceptionhandler(traceback.format_exc())
def use_d3(H,pan):
tree = return_json(H,pan)
htmlx =htmls.htmlfile.replace('var flare','var flare = '+json.dumps(tree)+';')
with open('connection_chart.html', 'w', encoding ='utf-8') as file:
file.write(htmlx)
答案 0 :(得分:0)
我通过将参数block = False传递给plt.show()来解决了这个问题