使用 wx.SplitterWindow 时如何在 wxPython 中显示光标坐标

how to show the cursor coordinates in wxPython when using wx.SplitterWindow

我正在尝试做一些与此 link 中显示的类似的事情来绘制光标并在状态栏中报告数据坐标。但是,我的代码有点不同,因为我需要使用 wx.SplitterWindow 来分隔按钮和绘图。基本上,我在主框架模块中创建状态栏,但绘图是在单独的模块中生成的。请参阅下面我当前的代码:

from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import \
    FigureCanvasWxAgg as FigCanvas, \
    NavigationToolbar2WxAgg as NavigationToolbar, \
    wxc as wxc

import pylab
import wx
from numpy import arange, sin, pi

class data:
    def __init__(self):
        self.t = []
        self.s = []

class Plot_Panel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)

        # create some sizers
        mainSizer = wx.BoxSizer(wx.VERTICAL)
        checkSizer = wx.BoxSizer(wx.HORIZONTAL)

        # create figrue
        self.fig = Figure()
        self.canvas = FigCanvas(self, -1, self.fig)
        self.axes = self.fig.add_subplot(111)

        # create the widgets
        self.toggleMarker = wx.CheckBox(self, label="Show Marker")

        # layout the widgets
        mainSizer.Add(self.canvas, 1, wx.EXPAND)
        checkSizer.Add(self.toggleMarker, 0, wx.ALL, 5)
        mainSizer.Add(checkSizer)
        self.SetSizer(mainSizer)

    def draw_plot(self, data):
        # Clear the previous figure
        self.fig.clear()

        # Redraw figure
        self.axes = self.fig.add_subplot(111)

        # Define data to plot
        self.plot_data= self.axes.plot(data.t, data.s, linewidth=3, color='y',)[0]

        # Draw Cursor or not
        if self.toggleMarker.IsChecked():
            # Note that event is a MplEvent
            self.canvas.mpl_connect('motion_notify_event', self.UpdateStatusBar)
            self.canvas.Bind(wx.EVT_ENTER_WINDOW, self.ChangeCursor)

        self.canvas.draw()

    def ChangeCursor(self, event):
        self.canvas.SetCursor(wxc.StockCursor(wx.CURSOR_BULLSEYE))

    def UpdateStatusBar(self, event):
        if event.inaxes:
            x, y = event.xdata, event.ydata
            # self.statusBar.SetStatusText(("x= "+str(Pos.x)+"  y="+str(Pos.y)))


class Button_Panel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)

        # create the widgets
        self.toggleStart = wx.Button(self, id=wx.ID_ANY, label="Plot data")


class ProportionalSplitter(wx.SplitterWindow):
    def __init__(self,parent, id = -1, proportion=0.66, size = wx.DefaultSize, **kwargs):
        wx.SplitterWindow.__init__(self,parent,id,wx.Point(0, 0),size, **kwargs)
        self.SetMinimumPaneSize(50) #the minimum size of a pane.
        self.proportion = proportion
        if not 0 < self.proportion < 1:
            raise ValueError, "proportion value for ProportionalSplitter must be between 0 and 1."
        self.ResetSash()
        self.Bind(wx.EVT_SIZE, self.OnReSize)
        self.Bind(wx.EVT_SPLITTER_SASH_POS_CHANGED, self.OnSashChanged, id=id)
        self.Bind(wx.EVT_PAINT, self.OnPaint)
        self.firstpaint = True

    def SplitHorizontally(self, win1, win2):
        if self.GetParent() is None: return False
        return wx.SplitterWindow.SplitHorizontally(self, win1, win2,
            int(round(self.GetParent().GetSize().GetHeight() * self.proportion)))

    def SplitVertically(self, win1, win2):
        if self.GetParent() is None: return False
        return wx.SplitterWindow.SplitVertically(self, win1, win2,
            int(round(self.GetParent().GetSize().GetWidth() * self.proportion)))

    def GetExpectedSashPosition(self):
        if self.GetSplitMode() == wx.SPLIT_HORIZONTAL:
            tot = max(self.GetMinimumPaneSize(),self.GetParent().GetClientSize().height)
        else:
            tot = max(self.GetMinimumPaneSize(),self.GetParent().GetClientSize().width)
        return int(round(tot * self.proportion))

    def ResetSash(self):
        self.SetSashPosition(self.GetExpectedSashPosition())

    def OnReSize(self, event):
        "Window has been resized, so we need to adjust the sash based on self.proportion."
        self.ResetSash()
        event.Skip()

    def OnSashChanged(self, event):
        "We'll change self.proportion now based on where user dragged the sash."
        pos = float(self.GetSashPosition())
        if self.GetSplitMode() == wx.SPLIT_HORIZONTAL:
            tot = max(self.GetMinimumPaneSize(),self.GetParent().GetClientSize().height)
        else:
            tot = max(self.GetMinimumPaneSize(),self.GetParent().GetClientSize().width)
        self.proportion = pos / tot
        event.Skip()

    def OnPaint(self, event):
        if self.firstpaint:
            if self.GetSashPosition() != self.GetExpectedSashPosition():
                self.ResetSash()
            self.firstpaint = False
        event.Skip()

class Main_Window(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, title = title)

        # Create a StatusBar at the bottom of the window
        self.statusBar = wx.StatusBar(self, -1)
        self.SetStatusBar(self.statusBar)

        # Set plot panel
        self.splitter = ProportionalSplitter(self,-1, 0.85)
        self.ppanel = Plot_Panel(self.splitter)
        self.ppanel.SetBackgroundColour('#ffffff')

        # Set button panel                           
        self.bpanel = Button_Panel(self.splitter)

        # Set frame 
        self.splitter.SplitVertically(self.ppanel, self.bpanel)
        self.Show(True)
        self.Maximize(True)  

        # bind the widgets
        self.ppanel.toggleMarker.Bind(wx.EVT_CHECKBOX, self.onToggleMarker)
        self.bpanel.toggleStart.Bind(wx.EVT_BUTTON, self.onToggleStart)        

        # Set classes
        self.data = data()


    def onToggleMarker(self, event):
        self.ppanel.draw_plot(self.data)

    def onToggleStart(self, event):

        self.data.t = arange(0.0, 1.0, 0.01)
        self.data.s = sin(2*2*pi*self.data.t)

        # plot data
        self.ppanel.draw_plot(self.data)


def main():
    app = wx.App(False)
    frame = Main_Window(None, "GUI")
    frame.Show()
    app.MainLoop()


if __name__ == "__main__" :
    main()

按下按钮 "Plot data" 时会显示绘图。我想做的是在选中复选框 "Show Marker" 时在状态栏中显示 x 和 y 位置(与在 link 中发布的代码中所做的方式类似),并在未选中时停止。但是我不确定是否可以在我的代码中执行此操作,因为状态栏和绘图的定义在不同的模块中。欢迎任何提示。

很高兴能得到一个完整的、有问题的示例程序。
您只需要将基本模块作为参数传递给 self.ppanel

self.ppanel = Plot_Panel(self.splitter, self)

然后更新状态栏的时候参考一下,下面参考base

from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import \
    FigureCanvasWxAgg as FigCanvas, \
    NavigationToolbar2WxAgg as NavigationToolbar, \
    wxc as wxc

import pylab
import wx
from numpy import arange, sin, pi

class data:
    def __init__(self):
        self.t = []
        self.s = []

class Plot_Panel(wx.Panel):
    def __init__(self, parent, base):
        wx.Panel.__init__(self, parent)
        self.base = base
        # create some sizers
        mainSizer = wx.BoxSizer(wx.VERTICAL)
        checkSizer = wx.BoxSizer(wx.HORIZONTAL)

        # create figrue
        self.fig = Figure()
        self.canvas = FigCanvas(self, -1, self.fig)
        self.axes = self.fig.add_subplot(111)

        # create the widgets
        self.toggleMarker = wx.CheckBox(self, label="Show Marker")

        # layout the widgets
        mainSizer.Add(self.canvas, 1, wx.EXPAND)
        checkSizer.Add(self.toggleMarker, 0, wx.ALL, 5)
        mainSizer.Add(checkSizer)
        self.SetSizer(mainSizer)

    def draw_plot(self, data):
        # Clear the previous figure
        self.fig.clear()

        # Redraw figure
        self.axes = self.fig.add_subplot(111)

        # Define data to plot
        self.plot_data= self.axes.plot(data.t, data.s, linewidth=3, color='y',)[0]

        # Draw Cursor or not
        if self.toggleMarker.IsChecked():
            # Note that event is a MplEvent
            self.canvas.mpl_connect('motion_notify_event', self.UpdateStatusBar)
            self.canvas.Bind(wx.EVT_ENTER_WINDOW, self.ChangeCursor)

        self.canvas.draw()

    def ChangeCursor(self, event):
        self.canvas.SetCursor(wxc.StockCursor(wx.CURSOR_BULLSEYE))

    def UpdateStatusBar(self, event):
        if event.inaxes:
            x, y = event.xdata, event.ydata
            self.base.statusBar.SetStatusText(("x= "+str(x)+"  y="+str(y)))

class Button_Panel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)

        # create the widgets
        self.toggleStart = wx.Button(self, id=wx.ID_ANY, label="Plot data")


class ProportionalSplitter(wx.SplitterWindow):
    def __init__(self,parent, id = -1, proportion=0.66, size = wx.DefaultSize, **kwargs):
        wx.SplitterWindow.__init__(self,parent,id,wx.Point(0, 0),size, **kwargs)
        self.SetMinimumPaneSize(50) #the minimum size of a pane.
        self.proportion = proportion
        if not 0 < self.proportion < 1:
            raise ValueError, "proportion value for ProportionalSplitter must be between 0 and 1."
        self.ResetSash()
        self.Bind(wx.EVT_SIZE, self.OnReSize)
        self.Bind(wx.EVT_SPLITTER_SASH_POS_CHANGED, self.OnSashChanged, id=id)
        self.Bind(wx.EVT_PAINT, self.OnPaint)
        self.firstpaint = True

    def SplitHorizontally(self, win1, win2):
        if self.GetParent() is None: return False
        return wx.SplitterWindow.SplitHorizontally(self, win1, win2,
            int(round(self.GetParent().GetSize().GetHeight() * self.proportion)))

    def SplitVertically(self, win1, win2):
        if self.GetParent() is None: return False
        return wx.SplitterWindow.SplitVertically(self, win1, win2,
            int(round(self.GetParent().GetSize().GetWidth() * self.proportion)))

    def GetExpectedSashPosition(self):
        if self.GetSplitMode() == wx.SPLIT_HORIZONTAL:
            tot = max(self.GetMinimumPaneSize(),self.GetParent().GetClientSize().height)
        else:
            tot = max(self.GetMinimumPaneSize(),self.GetParent().GetClientSize().width)
        return int(round(tot * self.proportion))

    def ResetSash(self):
        self.SetSashPosition(self.GetExpectedSashPosition())

    def OnReSize(self, event):
        "Window has been resized, so we need to adjust the sash based on self.proportion."
        self.ResetSash()
        event.Skip()

    def OnSashChanged(self, event):
        "We'll change self.proportion now based on where user dragged the sash."
        pos = float(self.GetSashPosition())
        if self.GetSplitMode() == wx.SPLIT_HORIZONTAL:
            tot = max(self.GetMinimumPaneSize(),self.GetParent().GetClientSize().height)
        else:
            tot = max(self.GetMinimumPaneSize(),self.GetParent().GetClientSize().width)
        self.proportion = pos / tot
        event.Skip()

    def OnPaint(self, event):
        if self.firstpaint:
            if self.GetSashPosition() != self.GetExpectedSashPosition():
                self.ResetSash()
            self.firstpaint = False
        event.Skip()

class Main_Window(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, title = title)

        # Create a StatusBar at the bottom of the window
        self.statusBar = wx.StatusBar(self, -1)
        self.SetStatusBar(self.statusBar)

        # Set plot panel
        self.splitter = ProportionalSplitter(self,-1, 0.85)
        self.ppanel = Plot_Panel(self.splitter, self)
        self.ppanel.SetBackgroundColour('#ffffff')

        # Set button panel
        self.bpanel = Button_Panel(self.splitter)

        # Set frame
        self.splitter.SplitVertically(self.ppanel, self.bpanel)
        self.Show(True)
        self.Maximize(True)

        # bind the widgets
        self.ppanel.toggleMarker.Bind(wx.EVT_CHECKBOX, self.onToggleMarker)
        self.bpanel.toggleStart.Bind(wx.EVT_BUTTON, self.onToggleStart)

        # Set classes
        self.data = data()


    def onToggleMarker(self, event):
        self.ppanel.draw_plot(self.data)

    def onToggleStart(self, event):

        self.data.t = arange(0.0, 1.0, 0.01)
        self.data.s = sin(2*2*pi*self.data.t)

        # plot data
        self.ppanel.draw_plot(self.data)


def main():
    app = wx.App(False)
    frame = Main_Window(None, "GUI")
    frame.Show()
    app.MainLoop()


if __name__ == "__main__" :
    main()