模态对话框冻结整个应用程序

时间:2013-05-29 13:47:54

标签: python matplotlib wxpython

我在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)上的一个bug,用于以下libs版本:  wx。版本:'2.8.12.1' matplotlib。版本:'1.1.1rc'

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分: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事件传递。