1

我在 ubuntu 机器上完全使用 wxpython 和 matplotlib 后端。我想将我的 matplotlib 画布连接到一个弹出 wxpython 模式对话框的 button_press_event。当模态对话框弹出时,整个应用程序被冻结。Windows 机器上不会出现此问题。这是一个通常会重现问题的片段。

import wx

from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg
from matplotlib.figure import Figure


class SettingDialog(wx.Dialog):

    def __init__(self, parent=None):

        wx.Dialog.__init__(self, parent, wx.ID_ANY, title="Modal dialog")


class PlotterFrame(wx.Frame):

    def __init__(self, parent, title="Frame with matplotlib canvas"):

        wx.Frame.__init__(self, parent, wx.ID_ANY, title)

        self.figure = Figure(figsize=(5,4), dpi=None)
        self.canvas = FigureCanvasWxAgg(self, -1, self.figure )
        self.canvas.mpl_connect("button_press_event", self.on_click)


    def on_click(self, event=None):
        d = SettingDialog(self)
        d.ShowModal()
        d.Destroy()  

if __name__ == "__main__":
    app = wx.App(False)
    f = PlotterFrame(None)
    f.Show()
    app.MainLoop()

你知道我的代码有什么问题吗?

PS0:问题是对话窗口也被冻结了,就像桌面上的所有应用程序都不再反应一样。唯一的逃生方法是使用键盘切换到另一个桌面

PS1:像http://eli.thegreenplace.net/files/prog_code/wx_mpl_bars.py.txt这样 的一个非常常见的例子,问题也出现了,我得出这样的结论,这个问题是linux上的一个错误(这里是ubuntu 12.04)以下库版本:wx. 版本:“2.8.12.1”matplotlib。版本:'1.1.1rc'

4

2 回答 2

1

我在几个不同的 Linux 系统上也遇到了这个问题。提到的各种资源似乎都没有描述与这个问题完全相同的内容。经过一番调查,当您尝试在 Matplotlib FigureCanvas 中触发鼠标释放事件之前显示模态对话框时,似乎有些东西被锁定了。

一旦我意识到这一点,解决方案就很简单了。您的事件处理程序应变为:

def on_click(self, event=None):
    try:
        event.guiEvent.GetEventObject().ReleaseMouse()
    except:
        pass
    d = SettingDialog(self)
    d.ShowModal()
    d.Destroy()

可能使代码复杂化的一个问题是并非所有 matplotlib 事件都具有相同的结构。所以如果这是一个'pick_event'处理程序,你会改为

event.mouseevent.guiEvent.GetEventObject().ReleaseMouse()

检查http://matplotlib.org/users/event_handling.html以获取哪些 matplotlib 事件传入的事件类型的键。

于 2013-10-01T04:21:49.703 回答
1

模态对话框的全部意义在于它在对话框处于模态状态时冻结应用程序。如果您不希望应用程序冻结,则不要以模态方式显示对话框。

于 2013-05-29T15:56:18.927 回答