[英]python matplotlib: Plotting to GUI in non-main thread
I had some issues with matplotlib runing in a non-main thread..我在非主线程中运行 matplotlib 时遇到了一些问题。
I'm doing a GUI with tkinter that runs some operations in a new thread, showing a progress bar, etc.. then plot graphics.我正在使用 tkinter 做一个 GUI,它在新线程中运行一些操作,显示进度条等。然后是 plot 图形。
The problem is: the plot part just doesnt work and I that warning comes:问题是: plot 部分不起作用,我警告来了:
UserWarning: Starting a Matplotlib GUI outside of the main thread will likely fail.用户警告:在主线程之外启动 Matplotlib GUI 可能会失败。
There's a solution?有解决办法吗?
Maybe if I can't call main thread back, can I run the matplotlib part in the main thread calling it's function in this non-main thread part of the code?也许如果我不能回调主线程,我可以在主线程中运行 matplotlib 部分,在代码的这个非主线程部分调用它的 function 吗?
All GUI function calls must happen from the main thread.所有 GUI function 调用必须从主线程发生。
You may be able to compute graphics in another thread and communicate the results to the main thread to be displayed.您可能能够在另一个线程中计算图形并将结果传达给要显示的主线程。
See https://docs.python.org/3/library/queue.html for inter-thread communication.有关线程间通信,请参阅https://docs.python.org/3/library/queue.html 。 In short, you can Queue.put the result from the secondary thread and Queue.get the result in the main thread.简而言之,您可以 Queue.put 从辅助线程中获取结果,并在主线程中 Queue.get 中获取结果。
Matplotlib is not thread-safe , you could use Multiprocessing instead and have another process handle the graphics plotting. Matplotlib 不是线程安全的,您可以改用多处理并让另一个进程处理图形绘图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.