Cannot move Matplotlib plot window and exit it using red X button

假装没事ソ 提交于 2019-12-20 03:56:13

问题


I'm running Python v3.5 and matplotlib v1.4.3 on Windows 10 Home. Up to recently, I write the python script using matplotlib with PyQt GUI. The 'plt.show()' code will be written in another module not __main__. When I run this code, Matplotlib figure cannot be moved and exit using red button X at the top of the right side of figure. Strangely, The chart is shown and It really does work well.

Why does this symptom happens? and How can I revise it?


回答1:


I stumbled on a similar problem. This is because your matplotlib figure and your PyQt GUI are both running in the same main thread. Since they are in the main thread, only one of them has the CPU for itself.

I have tried to solve the problem by putting either the PyQT GUI or the matplotlib inside another thread. But that doesn't work. Both PyQT and matplotlib need to run in the main thread.

So here is a workaround. You can start the matplotlib figure from a newly created python shell:

import subprocess
...

    # When you click the button in your PyQT GUI,
    # Create a new process:
    myMatProcess = subprocess.Popen(['python', 'myGraph.py'], shell=True)

Now your PyQT GUI and the matplotlib figure you're drawing have their own python interpreter shell. And they can run smoothly without blocking each other.

If you have any further questions, don't hesitate to ask. I'd be happy to help you out.



来源:https://stackoverflow.com/questions/36675269/cannot-move-matplotlib-plot-window-and-exit-it-using-red-x-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!