简体   繁体   English

wxPython和matplotlib.widgets

[英]wxPython and matplotlib.widgets

I'm working on a Python app that uses wxPython for a GUI with an embedded matplotlib figure. 我正在开发一个Python应用程序,它使用wxPython作为带有嵌入式matplotlib图形的GUI。 It was going really well until I tried to use a matplotlib GUI-neutral widget called a SpanSelector . 在我尝试使用名为SpanSelector的matplotlib GUI中性窗口小部件之前,它确实很顺利。 Before, I was trying to code this behavior myself, which wasn't going very well, so I was pleased that there was a built-in widget that would take care of it for me. 在此之前,我试图自己编写这种行为,这种行为并不顺利,所以我很高兴有一个内置的小部件可以为我处理它。 These widgets are supposed to work with any GUI backend, and wxPython is definitely a supported backend. 这些小部件应该适用于任何GUI后端,而wxPython绝对是受支持的后端。

My code is pretty simple at the moment; 我的代码目前非常简单; I have a defined application class with these (relevant) functions: 我有一个定义的应用程序类与这些(相关)函数:

def init_gui(self):
    ...
    self.button5 = wx.Button(tab, label="Manual select", size=(120, 30))
    self.button5.Bind(wx.EVT_BUTTON, self.get_selection_manual)
    ...

def get_selection_manual(self, event):
    span = matplotlib.widgets.SpanSelector(self.ax, \
        self.process_selection_manual, "horizontal", useblit=True, \
        rectprops=dict(alpha=0.5, facecolor="red"))

def process_selection_manual(self, vmin, vmax):
    print vmin, vmax

If it was working, all it should do is print out the selection the user made after clicking the button. 如果它正常工作,它所要做的就是打印出用户点击按钮后所做的选择。 I know that get_selection_manual is called, but clicking and dragging over the plot never creates a selection, and the callback process_selection_manual is never called. 我知道调用了get_selection_manual ,但是单击并拖动绘图永远不会创建选择,并且永远不会调用回调process_selection_manual

I tried putting in a sleep() and then updating the display. 我尝试进入sleep()然后更新显示。 Interestingly, I got it to (sort of) work when my application crashed over a sleep(5) instead of a time.sleep(5) . 有趣的是,当我的应用程序在sleep(5)而不是time.sleep(5)崩溃时,我得到它(有点)工作。 So Python was stalled, but it drew the selection like it was supposed to then. 所以Python已经停滞不前了,但它的选择就像它应该的那样。 I've never seen that happen before, and I'm not really sure what it means. 我以前从未见过这种情况,我不确定这意味着什么。 I haven't found any examples of using wxPython and matplotlib widgets together, so any help is greatly appreciated. 我没有找到任何使用wxPython和matplotlib小部件的例子,所以非常感谢任何帮助。

This turned out to be a really simple solution: span has to be declared self.span ... Maybe it was getting garbage-collected before it could do anything? 事实证明这是一个非常简单的解决方案: span必须被声明为self.span ...也许它在它可以做任何事情之前被垃圾收集了? In any case, it works now if I replace 在任何情况下,如果我更换,它现在工作

def get_selection_manual(self, event):
    span = matplotlib.widgets.SpanSelector(self.ax, \
        self.process_selection_manual, "horizontal", useblit=True, \
        rectprops=dict(alpha=0.5, facecolor="red"))

with

def get_selection_manual(self, event):
    self.span = matplotlib.widgets.SpanSelector(self.ax, \
        self.process_selection_manual, "horizontal", \
        rectprops=dict(alpha=0.5, facecolor="red"))

I also removed the useblit flag because it messed up the color; 我还删除了useblit标志,因为它弄乱了颜色; I don't think it was relevant to this fix, but still useful to note. 我不认为这与此修复有关,但仍然有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM