我如何获取光标坐标并在wxpython中的其他面板中使用鼠标位置

时间:2019-06-12 12:32:47

标签: python-3.x wxpython

我试图放置一个可以移动并可以显示图像的坐标或像素的光标和矩形补丁,然后选择并显示在textctrl中的另一个面板Paneltwo上 每次我移动鼠标(自动矩形)时,每个点的像素都会显示在另一个面板上

矩形不能用鼠标移动的第一个问题! 第二个我需要做的是当用鼠标移动矩形时,我可以在两个textctrl面板中可视化鼠标(矩形)的像素或位置! 我该怎么办?

那部分代码:

 import wx
from numpy import arange, sin, pi,cos
import numpy as np
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.widgets import RectangleSelector
from matplotlib.figure import Figure

class MainFrame(wx.Frame):
    def __init__(self, parent ):
        wx.Panel.__init__(self, parent,name="Main", size = (600,400))
        self.Panel = Panel(self)




class Panel(wx.Panel):
    def __init__(self,parent):
        super().__init__(parent)
        panel = wx.Panel(self)
        self.panel_two = PanelTwo(parent=self)
        self.canvas_panel = CanvasPanel(self)
        canvas_sizer = wx.BoxSizer(wx.HORIZONTAL)
        canvas_sizer.Add(self.canvas_panel,1,wx.EXPAND)
        canvas_sizer.Add(self.panel_two,1,wx.EXPAND)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(panel)
        sizer.Add(canvas_sizer)
        self.SetSizerAndFit(sizer)
        self.Show()

class CanvasPanel(wx.Panel):
    """ Panel du graphique matplotlib """
    def __init__(self, parent , size=(200,250)):
        super().__init__(parent)
        self.figure = Figure(figsize =(4,3))
        self.canvas = FigureCanvas(self, -1, self.figure)
        self.axes = self.figure.add_subplot(111)
        self.Size = self.canvas.Size
        self.parent = parent
        t = arange(0.5, 3.0, 0.01)
        s = cos(2 * pi * t)
        self.axes.plot(t, s)



     #can'tmove rectangel with mouse    
    def on_press(self,event):
        xpress, ypress = event.xdata, event.ydata
        w = rect.get_width()
        h = rect.get_height()
        rect.set_xy((xpress-w/2, ypress-h/2))

        ax.lines = []   
        ax.axvline(xpress, c='b')
        ax.axhline(ypress, c='b')

        self.fig.canvas.draw()


        self.fig = plt.figure

        self.axes = plt.subplot(111)
        self.axes.imshow(t,s)

        self.fig.canvas.mpl_connect('button_press_event',on_press)


        self.rect = patches.Rectangle((x,y),0.01,0.01,linewidth=1,edgecolor='g',facecolor='none')

        self.axes.add_patch(rect)

        self.plt.show()

class PanelTwo(wx.Panel): #here when i need to visualize pixel and coordinator cursor
    def __init__(self,parent):
        wx.Panel.__init__(self,parent,size=(200,250))

        self.text_ctrl = wx.TextCtrl(self, -1, "", style=wx.TE_MULTILINE|wx.BORDER_SUNKEN|wx.TE_READONLY|
                                wx.TE_RICH2, size=(200,170), pos = (40,20))

        lbl = wx.StaticText(self,label="Coordinato cursor & Pixel " , pos=(40,0))

app = wx.App()
frame = MainFrame(None).Show()
app.MainLoop()

谢谢

1 个答案:

答案 0 :(得分:1)

尝试一下,看看它是否朝着正确的方向移动。
注意:我不知道您如何从该绘图中检索像素位置。
也许实际上知道他们在使用matplotlib做什么的其他人(因为我当然不知道)可以提供帮助。

import wx
from numpy import arange, sin, pi,cos
import numpy as np
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.figure import Figure
import matplotlib.patches as patches

class MainFrame(wx.Frame):
    def __init__(self, parent ):
        wx.Panel.__init__(self, parent,name="Main", size = (800,400))
        self.Panel = Panel(self)

class Panel(wx.Panel):
    def __init__(self,parent):
        super().__init__(parent)
        panel = wx.Panel(self)
        self.panel_two = PanelTwo(parent=self)
        self.canvas_panel = CanvasPanel(self)
        canvas_sizer = wx.BoxSizer(wx.HORIZONTAL)
        canvas_sizer.Add(self.canvas_panel,1,wx.EXPAND)
        canvas_sizer.Add(self.panel_two,1,wx.EXPAND)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(panel)
        sizer.Add(canvas_sizer)
        self.SetSizerAndFit(sizer)
        self.Show()

class CanvasPanel(wx.Panel):
    """ Panel du graphique matplotlib """
    def __init__(self, parent , size=(200,250)):
        super().__init__(parent)
        self.figure = Figure(figsize =(4,3))
        self.canvas = FigureCanvas(self, -1, self.figure)
        self.axes = self.figure.add_subplot(111)
        self.Size = self.canvas.Size
        self.parent = parent
        t = arange(0.5, 3.0, 0.01)
        s = cos(2 * pi * t)
        self.axes.plot(t, s)
        self.canvas.mpl_connect('button_press_event', self.on_press)
        x = y = 0.2
        self.rect = patches.Rectangle((x, y), 0.4, 0.4, alpha=1, fill=None, label='Label')
        self.axes.add_patch(self.rect)
        self.axes.plot()

    def on_press(self, click):
        x1, y1 = click.xdata, click.ydata
        self.parent.panel_two.Update(x1,y1)
        self.rect.set_x(x1 - 0.2) #Move the rectangle and centre it on the X click point
        self.rect.set_y(y1 - 0.2) #Move the rectangle and centre it on the Y click point
        self.axes.plot()
        self.canvas.draw()

class PanelTwo(wx.Panel): #here when i need to visualize pixel and coordinator cursor
    def __init__(self,parent):
        wx.Panel.__init__(self,parent,size=(300,250))

        self.text_ctrl = wx.TextCtrl(self, -1, "", style=wx.TE_MULTILINE|wx.BORDER_SUNKEN|wx.TE_READONLY|wx.TE_RICH2, size=(200,170))

        lbl = wx.StaticText(self,label="Coordinato cursor & Pixel ")
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(lbl,0, wx.ALIGN_CENTRE,10)
        sizer.Add(self.text_ctrl,0, wx.ALIGN_CENTRE,10)
        self.SetSizer(sizer)

    def Update(self,x1,y1):
        self.text_ctrl.SetValue("Mouse click at;\nX "+str(x1)+"\nY "+str(y1))

app = wx.App()
frame = MainFrame(None).Show()
app.MainLoop()

enter image description here