Issue
I've programmed a pendulum simmulation with pygame for a college project and i want to let the user see the graphs with a matplotlib window. When i run it from vscode it runs perfectly, but when i run it directly from terminal and try to see a graph it gives me:
Fatal Python error: PyEval_SaveThread: the function must be called with the GIL held, but the GIL is released (the current Python thread state is NULL)
Python runtime state: initialized
Current thread 0x00007f00b05ac740 (most recent call first):
File "/usr/lib/python3/dist-packages/gi/overrides/Gtk.py", line 1649 in main
File "/home/estevao/.local/lib/python3.9/site-packages/matplotlib/backends/backend_gtk3.py", line 850 in mainloop
File "/home/estevao/.local/lib/python3.9/site-packages/matplotlib/backend_bases.py", line 3595 in show
File "/home/estevao/.local/lib/python3.9/site-packages/matplotlib/pyplot.py", line 378 in show
File "/home/estevao/Projetos/Pendulum/Estev\xe3o_Projeto1.py", line 27 in export_graph
File "/home/estevao/.local/lib/python3.9/site-packages/pygame_menu/widgets/core/widget.py", line 1230 in apply
File "/home/estevao/.local/lib/python3.9/site-packages/pygame_menu/widgets/widget/button.py", line 247 in update
File "/home/estevao/.local/lib/python3.9/site-packages/pygame_menu/menu.py", line 2322 in update
File "/home/estevao/Projetos/Pendulum/Estev\xe3o_Projeto1.py", line 325 in <module>
Do you have any idea how i could fix it? I can upload the entire code but its full of mess and large enough to make you less likely to help me if you see it.
Solution
Actually I got to solve it changing the backend of matplotlib and using multiprocessing. So the changes I made in the code were
import matplotlib
matplotlib.use('TkAgg')
import multiprocessing
and
work = multiprocessing.Process(target=show_graph)
work.start()
Where 'show_graph' is the function which generate and show the graph with matplotlib.
Answered By - sbb
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.