python - 用于 GDB python pretty-print 的非阻塞 pyplot GUI

标签 python matplotlib tkinter gdb pretty-print

我只是想在调试的时候用matplotlib可视化一些数据。关注此页面:Analyzing C/C++ matrix in the gdb debugger with Python and Numpy - CodeProject , 它工作正常,除非 matplotlib GUI 只是阻止 GDB 的命令行。这意味着如果我让 GUI 窗口保持打开状态,GDB 的命令行将被卡住,并且在关闭 pyplot 窗口之前我无法在 GDB 命令中输入任何内容。

为了解决这个问题,我只是尝试在另一个线程中运行绘图代码,为了简化测试用例,我只是创建了一个简单的 python 源代码,名为“test-pyplot.py”,内容如下

import numpy as np
from matplotlib import pyplot as plt
from threading import Thread

class MyThread (Thread):

    def __init__(self, thread_id):
        Thread.__init__(self)
        self.thread_id = thread_id

    def run(self):
        x = np.arange(0, 5, 0.1);
        y = np.sin(x)
        plt.plot(x, y)
        plt.show(block = True) #this cause the mainloop

thread1 = MyThread(1)
thread1.start()

现在,在 GDB 命令行下,我只需输入:source test-pyplot.py,就会打开一个非阻塞的 GUI,看起来不错,GDB 的命令行仍然可以接受命令,到目前为止一切顺利。

但是当我关闭绘图窗口时,问题发生了,然后我再次运行 source test-pyplot.py,这一次,GDB 只是挂起。

我在 Windows 下使用 python 2.7.6,我看到 matplotlib 默认使用 tkAgg 作为绘图后端,所以我尝试看看这是否会发生在正常情况下tk 图形用户界面窗口。这是另一个名为“test-tk.py”的测试 python 文件,其内容如下:

from Tkinter import *
from threading import Thread
class App():
    def __init__(self):
        self.g=Tk()
        self.th=Thread(target=self.g.mainloop)
        self.th.start()
    def destroy(self):
        self.g.destroy()
a1 = App()

如果我在 GDB 提示符下运行命令 source test-tk.py,会出现一个 tk 窗口,GDB 仍然存在(未卡住),我可以关闭 tk 窗口,并且再次输入命令 source test-tk.py,一切正常,GDB 没有挂起。我什至可以在不关闭第一个 tk 窗口的情况下运行命令 source test-tk.py 两次,然后将显示两个 tk 窗口。

问题:如何在非阻塞模式下正确显示matplotlib pyplot图形,不挂GDB?谢谢。 通常情况下,plt.show会在内部调用Tkinter包的mainloop函数,这是一个事件循环。 matplotlib 确实有一个名为交互模式的选项,可以通过调用 `plt.ion()' 来启用它,但它没有解决我的问题。

最佳答案

存在多个问题。 FWIW 我想我之前调查过这个确切的问题,搜索 gdb bugzilla。

无论如何,一个问题是您可能应该在单独的线程中运行 GUI。这避免了缺少主循环集成。这有点难编程,但您可以使用 Python 技巧来减轻它的痛苦。

此外,GUI 工具包通常会干扰 SIGCHLD,这会破坏 gdb。所以你必须使用 hack 来处理这个问题。

您可以在我的概念验证 gdb GUI 中看到我是如何处理这些问题的:https://gitorious.org/gdb-gui

关于python - 用于 GDB python pretty-print 的非阻塞 pyplot GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24924357/

相关文章:

python - 如何使用 webapp2 添加 auth_id?

python - 将我的 'next' url 存储在签名的 cookie 中并无忧无虑地重定向到它是否安全?

python - 如何在 django 中创建单个帖子和 url 详细信息的 View ?

python - Matplotlib - 在子图之间共享轴时缺少一些刻度

python - 如何在 Matplotlib 中使用不同主网格间距的 xticklabel?

python - 如何在 matplotlib 中使用交互式事件编辑表格数据

python - 仅使用一张深度图像的 OpenCV 视差图后过滤

python - 为什么 Tkinter.TopLevel.wm_geometry() 返回 "1x1+0+0"?

python - Tkinter GUI 组合

python - 在 Python 中使用滚动条显示大图像