Rohit Raj
Rohit Raj

Reputation: 339

tkinter -Fatal Python error: PyEval_RestoreThread: NULL tstate

In tkinter gui I have created, if I try to write a text file while matplotlib plot is open in a tkinter window then python throws following error:

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>

If I close the tkinter window containing the plot and then execute the gui function for writing text file then no error happens

Function for creating matplotlib plot

function containing line 40 in connections.py as in above traceback

def draw_connected_pans(G):
    Pos = nx.spring_layout(G)
    nx.draw_networkx(G, pos = Pos)
    plt.axis('off')
    plt.show()

function containing line 1325 in tlscript.py as in above traceback

def draw_all_pans():
        try:
            application_window = window
            connections.draw_connected_pans(graph)
        except:
            exceptionhandler(traceback.format_exc())

Code which triggers the bug

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)

Upvotes: 0

Views: 2515

Answers (1)

Rohit Raj
Rohit Raj

Reputation: 339

I solved it by passing argument block=False to plt.show()

Upvotes: 1

Related Questions